136. Single Number
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?
本题可以用missing number的方法来做,因为数组里面除了某一个元素外,其余都出现两次,代码如下:
public class Solution {
public int singleNumber(int[] nums) {
int xor = 0;
for(int i=0;i<nums.length;i++){
xor = xor^nums[i];
}
return xor;
}
}