-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFirstMissingPositive.java
More file actions
56 lines (52 loc) · 1.5 KB
/
FirstMissingPositive.java
File metadata and controls
56 lines (52 loc) · 1.5 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package collection3;
public class FirstMissingPositive {
public static void pre (int n){
int result = 1;
for(int i=2;i<=n;i++){
if(i!=4)
result=i;
}
System.out.println(result);
}
public static int firstMissingPositive(int[] A) {
if(A.length==0){
return 1;
}
for(int i =0;i<A.length;i++){
if(A[i]>0){
if(A[i]>A.length){
A[0] = A[i];
} else {
int temp = A[i]-1;
A[i] = A[temp];
A[temp] = temp+1;}
}else {
A[i] = 0;
}
}
for(int i = 0;i<A.length;i++){
System.out.print(A[i]+" ");
}
if(A[0]>1) return 1;
for(int i =1;i<A.length;i++){
if(A[i]!=i+1){
return i+1;
}
}
if(A[0]==1) return A.length+1;
return A.length;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//for(int i=2;i<12;i++){pre(i);}
System.out.println(firstMissingPositive(new int[] {2}));
System.out.println(firstMissingPositive(new int[] {1}));
System.out.println(firstMissingPositive(new int[] {1,2,3,4}));
System.out.println(firstMissingPositive(new int[] {2,2}));
System.out.println(firstMissingPositive(new int[] {3,2}));
System.out.println(firstMissingPositive(new int[] {1,3,4}));
System.out.println(firstMissingPositive(new int[] {3,4,-1,1}));
System.out.println(firstMissingPositive(new int[] {1000,-1}));
System.out.println(firstMissingPositive(new int[] {1,1000}));
}
}