Question
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
.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
Solution
Result: Accepted Time: 16 ms
Here should be some explanations.
int missingNumber(int* nums, int numsSize) {
for(int i = 0; i < numsSize; i++)
nums[i] <<= 1;
for(int i = 0; i < numsSize; i++)
nums[nums[i]>>1] |= 1;
int ans = numsSize ;
for(int i = 0; i < numsSize; i++)
{
if(!(nums[i]&1))
ans = i;
nums[i] >>= 1;
}
return ans;
}
Complexity Analytics
- Time Complexity:
- Space Complexity: