leetcode-Missing Number

Difficulty: Medium

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) {
        if(nums.empty())
            return -1;
        int size=nums.size()/32+1;
        
        int bitmap[size];
        fill(bitmap,bitmap+size,0);
        int maxNum=nums[0];
        for(auto &e:nums){
            bitmap[e/32]|=(1<<(e%32));
            maxNum=max(e,maxNum);
        }
        
        for(int i=0;i<maxNum;++i){
            if(bitmap[i/32]&(1<<(i%32)))
                continue;
            else            
                return i;
        }
        return maxNum+1;        
    }
};



你可能感兴趣的:(leetcode-Missing Number)