Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5fadf43

Browse files
committed
first
1 parent664a91c commit5fadf43

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

‎array/FirstMissingPositive.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packageAlgorithms.array;
2+
3+
publicclassFirstMissingPositive {
4+
publicstaticvoidmain(String[]strs) {
5+
int[]in = {1,2,0};
6+
//int[] in = {3,4,-1,1};
7+
System.out.println(firstMissingPositive(in));
8+
}
9+
10+
publicstaticintfirstMissingPositive(int[]A) {
11+
if (A ==null) {
12+
return0;
13+
}
14+
15+
intlen =A.length;
16+
for (inti =0;i <len;i++) {
17+
// 1. The number should be in the range.
18+
// 2. The number should be positive.
19+
// 注意:和将要交换的值不可以相同,否则会死循环
20+
while (A[i] <=len &&A[i] >0 &&A[A[i] -1] !=A[i]) {
21+
swap(A,i,A[i] -1);
22+
}
23+
}
24+
25+
for (inti =0;i <len;i++) {
26+
if (A[i] !=i +1) {
27+
returni +1;
28+
}
29+
}
30+
31+
returnlen +1;
32+
}
33+
34+
publicstaticvoidswap(int[]A,inti,intj) {
35+
inttmp =A[i];
36+
A[i] =A[j];
37+
A[j] =tmp;
38+
}
39+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp