My code:
public class Solution {
public int[] singleNumber(int[] nums) {
if (nums == null || nums.length == 0)
return null;
int result = 0;
for (int i = 0; i < nums.length; i++)
result ^= nums[i];
int w = 1;
while ((result & w) == 0)
w = w << 1;
int x1 = 0;
int x2 = 0;
for (int i = 0; i < nums.length; i++) {
if ((nums[i] & w) == 0)
x1 ^= nums[i];
else if ((nums[i] & w) != 0)
x2 ^= nums[i];
}
int[] ret = new int[2];
ret[0] = x1;
ret[1] = x2;
return ret;
}
}
My test result:
这次题目也是属于基本放弃的那种。具体做法看这个博客。
Bit manipulation 真的想不到啊。。
http://bookshadow.com/weblog/2015/08/17/leetcode-single-number-iii/
**
总结: Bit manipulation
**
Anyway, Good luck, Richardo!
看了答案做了出来。我的做法还真是不错。看了我也懂了。。
Anyway, Good luck, Richardo! -- 08/05/2016