4 Lines O(1) space O(n) time C++ solution without xor!


  • 11
    A

    class Solution {
    public:
    int missingNumber(vector<int>& nums) {

        int sum=0, s=nums.size();
        for(int i=0;i<nums.size();i++)
        {
            sum+=nums[i];
        }
        
        return (s*(s+1)/2)-sum;
        
    }
    

    };


Log in to reply
 

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