Title description
The array nums contains all integers from 0 to n, but one is missing. Please write code to find the missing integer. Can you do it in O(n) time?
Note: This question is slightly modified from the original question in the book
Example
示例 1:
输入:[3,0,1]
输出:2
示例 2:
输入:[9,6,4,2,3,5,7,0,1]
输出:8
Problem solving ideas
Use a for loop to XOR the number from 0-numSize-1 with the element of num[]. Don’t forget to add numsSize to get the unpaired number as a missing number
Code
int missingNumber(int* nums, int numsSize){
int result=numsSize;
for(int i=0;i<numsSize;i++){
result^=i^nums[i];
}return result;
}