Java swapping numbers to same index


  • 0
    J
    public class Solution {
    public int missingNumber(int[] nums) {
        for(int i = 0; i < nums.length; i++){
            if (nums[i] >= nums.length)
                continue;
            else if (nums[i] != i){
                int temp = nums[nums[i]];
                nums[nums[i]] = nums[i];
                nums[i] = temp;
                i--;
            }
        }
        
        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.