268. Missing Number
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?
class Solution {
public:
int missingNumber(vector<int>& nums) {
sort(nums.begin(),nums.end());
int value[nums.size()]={0},j=0,count=0;
for(int i=0;i<nums.size();i++)
{
if(j<nums.size()&&i<=nums[j])
{
if(nums[j]!=i)
{
value[count]=i;
count++;
}
else
{
j++;
}
}
}
if(count==0)
{
return nums.size();
}
else
{
for(int i=0;i<nums.size();i++)
{
return value[i];
}
}
}
};