LeetCode P136 Single Number 题解
1.题目:
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Subscribe to see which companies asked this question
题意:
输入一个数组,返回这个数组中单出来的那个数,希望只做一次循环不占用其它空间
2.解题思路:
java 位运算的^亦或:二进制每位相同则结果为0,不同则结果为1。
由定义可以知道:0^n=n,n^n=0;
3.代码
1 |
|
4.一些总结:
有时候位运算是一个不错的选择~不过好像就知道这一种符合要求的方法,
希望可以找到新的方法补充。