My java solution without sum constant memory and O(n)


  • 0
    S
    public class Solution {
    public int missingNumber(int[] nums) {
        
        int nindex=-1;
        int n = nums.length;
        for(int i=0;i<n;i++)
        {
            while(nums[i]!=i&&nums[i]!=n)
            {
                int tmp = nums[i];
                nums[i] = nums[nums[i]];
                nums[tmp] = tmp;
            }
            if(nums[i]==n)
                nindex=i;
        }
        
        return nindex==-1?n:nindex;
    }
    

    }


Log in to reply
 

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