My Java solution ammortized O(n)


  • 0
    Q
      public class Solution {
        public int missingNumber(int[] nums) {
           for (int i=0;i<nums.length;i++){
                while (nums[i]!=i){
                	if ( nums[i]<nums.length){
                		int temp = nums[ nums[i]];                
                    	nums[ nums[i]]=nums[i];
                    	nums[i]=temp;
                    }
                	else{
                	    break;
                	}
                }
            }
            for (int i=0;i<nums.length;i++){
                if (nums[i]!=i){
                    return i;
                }
            }
            return nums.length;
        }
    }

Log in to reply
 

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