Share my Java solution


  • 0
    T
    public int missingNumber(int[] nums) {
            int n = nums.length;
            for (int i = 0; i < n; i++) {
                if (nums[i] != i && nums[i] != n) {
                    int temp = nums[nums[i]];
                    nums[nums[i]] = nums[i];
                    nums[i] = temp;
                    i--;
                }
            }
            
            for (int i = 0; i < n; i++) {
                if (nums[i] == n) {
                    return i;
                }
            }
            
            return n;
        }

Log in to reply
 

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