- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstMissingPositive.java
More file actions
Latest commit
30 lines (28 loc) · 904 Bytes
/
Copy pathFirstMissingPositive.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
publicclassFirstMissingPositive {
/**
*
*/
publicintfirstMissingPositive(int[] nums) {
if (nums == null || nums.length == 0) return1;
inti = 0;
while (i < nums.length) {
System.out.println("what ? :" + nums.length);
if (nums[i] <= 0 || nums[i] > i + 1) i++;
elseif (nums[nums[i] - 1] != nums[i]) swap(nums, nums[i] - 1, i);
elsei++;
}
for (i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) returni + 1;
}
returnnums.length + 1;
}
privatevoidswap(int[] nums, inti, intj) {
inttmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
publicstaticvoidmain(String[] args) {
FirstMissingPositivea = newFirstMissingPositive();
System.out.println(a.firstMissingPositive(newint[] {3,4,-1,1}));
}
}