[leetcode]268. Missing Number

[leetcode]268. Missing Number


Analysis

paper的噩梦似乎又要开始了~—— [ummmm~]

Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
给一个大小为n的数组,判断数组里缺少的数字是哪个。

Implement

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int len = nums.size();
        sort(nums.begin(), nums.end());
        if(nums[len-1] != len)
            return len;
        for(int i=0; i<len; i++){
            if(nums[i] != i)
                return i;
        }
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/80697735