Missing Number【268】

Datetime:2016-08-23 01:22:05         Topic: LeetCode          Share        Original >>
Here to See The Original Article!!!

268. Missing Number

My Submissions

Total Accepted:  31740 Total Submissions:  83547 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?


Special thanks to  @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Tags
Array   Math   Bit Manipulation

class Solution {
    int missingNumber(vector<int>& nums) {
        int sum = 0;  
        for(int num: nums)
            sum += num;  
        int n = nums.size();  
        return (n * (n + 1))/ 2 - sum;  

About List