@Test void test1() { Assert.assertEquals(3, firstMissingPositive.firstMissingPositive(new int[]{1,2,0})); Assert.assertEquals(2, firstMissingPositive.firstMissingPositive(new int[]{3,4,-1,1})); Assert.assertEquals(1, firstMissingPositive.firstMissingPositive(new int[]{7,8,9,11,12})); }
public int firstMissingPositive(int[] nums) { int n = nums.length; if (n == 0) { return 1; } if (n == 1) { return nums[0] == 1 ? 2 : 1; } for (int i = 0; i < n; i++) { while (nums[i] > 0 && nums[i] < n && nums[i] != nums[nums[i]]) { swap(nums, i, nums[i]); } } for (int i = 1; i < n; i++) { if (nums[i] != i) { return i; } } return nums[0] == n ? n + 1 : n; }
FirstMissingPositive { public int firstMissingPositive(int[] nums) { int n = nums.length; if (n == 0) { return 1; } if (n == 1) { return nums[0] == 1 ? 2 : 1; } for (int i = 0; i < n; i++) { while (nums[i] > 0 && nums[i] < n && nums[i] != nums[nums[i]]) { swap(nums, i, nums[i]); } } for (int i = 1; i < n; i++) { if (nums[i] != i) { return i; } } return nums[0] == n ? n + 1 : n; } }
FirstMissingPositive { public int firstMissingPositive(int[] nums) { int n = nums.length; if (n == 0) { return 1; } if (n == 1) { return nums[0] == 1 ? 2 : 1; } for (int i = 0; i < n; i++) { while (nums[i] > 0 && nums[i] < n && nums[i] != nums[nums[i]]) { swap(nums, i, nums[i]); } } for (int i = 1; i < n; i++) { if (nums[i] != i) { return i; } } return nums[0] == n ? n + 1 : n; }  }
FirstMissingPositive { public int firstMissingPositive(int[] nums) { int n = nums.length; if (n == 0) { return 1; } if (n == 1) { return nums[0] == 1 ? 2 : 1; } for (int i = 0; i < n; i++) { while (nums[i] > 0 && nums[i] < n && nums[i] != nums[nums[i]]) { swap(nums, i, nums[i]); } } for (int i = 1; i < n; i++) { if (nums[i] != i) { return i; } } return nums[0] == n ? n + 1 : n; }  int firstMissingPositive(int[] nums); int firstMissingPositive2(int[] nums); }
FirstMissingPositive { public int firstMissingPositive(int[] nums) { int n = nums.length; if (n == 0) { return 1; } if (n == 1) { return nums[0] == 1 ? 2 : 1; } for (int i = 0; i < n; i++) { while (nums[i] > 0 && nums[i] < n && nums[i] != nums[nums[i]]) { swap(nums, i, nums[i]); } } for (int i = 1; i < n; i++) { if (nums[i] != i) { return i; } } return nums[0] == n ? n + 1 : n; }  int firstMissingPositive(int[] nums); int firstMissingPositive2(int[] nums);  }