leetcode #136 single number

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?


数组中,题目给定了一定至少存在一个数,所以不考虑特殊输入的情况

成对出现的数,要考虑使用^操作

1
2
3
4
5
6
7
8
class  {
public int singleNumber(int[] nums) {
for(int i=1; i<nums.length; i++){
nums[0] = nums[0]^nums[i];
}
return nums[0];
}
}