O(n) c++ solution !! neat and simple :)


  • 1
    R
    int missingNumber(vector<int>& a) {
        if(a.empty()) return 0;
        int n = a.size();
        int sum = (n*(n+1))/2;
        for(int i = 0; i < n ;i++){
            sum-=a[i];
        }
        return sum;
    }

Log in to reply
 

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