A simple solution, very easy for understanding~ C++ O(n)


  • 0
    K
    class Solution {
    public:
    // if only one number missing, just use n(n+1)/2 - sum_now
        int missingNumber(vector<int>& nums) {
            int sum = (nums.size()+1)*(0+nums.size())/2;
            int sum2 = 0,i=0;
            while(i < nums.size())
                sum2 += nums[i++];
            return sum-sum2;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.