-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstMissingPositive.java
More file actions
30 lines (28 loc) · 904 Bytes
/
FirstMissingPositive.java
File metadata and controls
30 lines (28 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class FirstMissingPositive {
/**
*
*/
public int firstMissingPositive(int[] nums) {
if (nums == null || nums.length == 0) return 1;
int i = 0;
while (i < nums.length) {
System.out.println("what ? :" + nums.length);
if (nums[i] <= 0 || nums[i] > i + 1) i++;
else if (nums[nums[i] - 1] != nums[i]) swap(nums, nums[i] - 1, i);
else i++;
}
for (i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) return i + 1;
}
return nums.length + 1;
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
public static void main(String[] args) {
FirstMissingPositive a = new FirstMissingPositive();
System.out.println(a.firstMissingPositive(new int[] {3,4,-1,1}));
}
}