An accepted solution with O(1) space complexity and adding


  • 1
    T
    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int n = nums.size();
            int r_sum;
            if(n%2 ==0)
                r_sum = n/2*(n+1);
            else
                r_sum=(n+1)/2*n;
            int sum = 0;
            for(int i =0;i< n;i++)
                sum+=nums[i];
            return r_sum-sum;
        }
    };

Log in to reply
 

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