Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
从0-N中取出不同的N-1个数,要我们返回这个缺失的数。
一共会有N-1对相同的数,所以可以使用亦或法来解。
class Solution {
public int missingNumber(int[] nums) {
if(nums.length==1)
return nums[0]==0? 1:0;
int result = nums.length;
for(int i = 0 ;i<nums.length;i++)
{
result^=i^nums[i];
}
return result;
}
}