classSolution{ publicintfirstMissingPositive(int[] nums){ if (nums == null || nums.length == 0) return1; int i = 0; // If the current value is in the range of (0,length) and it's not at its correct position, // swap it to its correct position. // Else just continue; while (i < nums.length){ if (nums[i] == i + 1 || nums[i] <= 0 || nums[i] > nums.length || nums[i] == nums[nums[i] - 1]) i++; else swap(nums, i, nums[i] - 1); } for (i = 0; i < nums.length; i++) if (nums[i] != i + 1) break; return i+1; } privatevoidswap(int[] nums, int x, int y){ int tmp = nums[x]; nums[x] = nums[y]; nums[y] = tmp; } }