leetcode解题报告(17):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());
        for(int i = 0; i != nums.size(); ++i){
            if(nums[i] != i)return i;
        }
        return nums.size();
    }
};
posted @ 2017-05-08 22:06  larryking  阅读(118)  评论(0编辑  收藏  举报