O(n) No XOR No MATH.


  • 0
    S
    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int i , ans = nums.size( ) ;
            for( i = 0 ; i < nums.size( ) ; i++ )
                nums[i]++ ;
            for( i = 0 ; i < nums.size( ) ; i++ )
            {
                int val = abs( nums[i] ) - 1 ;
                if( val < nums.size( ) )
                    nums[ val ] *= -1 ;
            }
            for( i = 0 ; i < nums.size( ) ; i++ )
                if( nums[i] > 0 )
                    ans = i ;
            return ans ;
        }
    };
    

Log in to reply
 

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