-
Notifications
You must be signed in to change notification settings - Fork 0
/
41.first-missing-positive.java
38 lines (37 loc) · 1.03 KB
/
41.first-missing-positive.java
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
31
32
33
34
35
36
37
38
class Solution {
public int firstMissingPositive(int[] nums) {
if(nums.length == 0 || nums.length == 1 && nums[0] != 1)
return 1;
int i = 0;
int n = nums.length+2;
for(i=0;i<nums.length;i++){
//System.out.println(i+" out "+(nums[i]>n)+" "+n);
if(nums[i] > nums.length || nums[i] <= 0)
nums[i] = n;
}
i = 0;
while(i<nums.length){
// System.out.println(i+" out "+nums[i]+" "+n);
if(nums[i] != n && nums[i]-1 != i){
// System.out.println(i+" in "+nums[i]+" "+n);
int t = nums[nums[i]-1];
if(t == nums[i]){
i++;
continue;
}
nums[nums[i]-1] = nums[i];
nums[i] = t;
continue;
}
i++;
}
int p = 1;
for(int num:nums){
if(num == p)
p++;
else
break;
}
return p;
}
}