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

Commit127db30

Browse files
committed
remove duplicates2
1 parentec77e43 commit127db30

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎array/RemoveDuplicates2.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packageAlgorithms.array;
2+
3+
publicclassRemoveDuplicates2 {
4+
publicstaticintremoveDuplicates(int[]A) {
5+
if (A ==null) {
6+
return0;
7+
}
8+
9+
if (A.length <=1) {
10+
returnA.length;
11+
}
12+
13+
intlen =1;
14+
15+
// 拷贝2次后就不再拷贝
16+
booleancanCopy =true;
17+
18+
for (inti =1;i <A.length;i++) {
19+
if (A[i] ==A[i -1]) {
20+
if (!canCopy) {
21+
continue;
22+
}
23+
canCopy =false;
24+
}else {
25+
canCopy =true;
26+
}
27+
28+
A[len++] =A[i];
29+
}
30+
31+
returnlen;
32+
}
33+
34+
publicstaticvoidmain(String[]strs) {
35+
int[]A = {1,1,1,2,2,3};
36+
removeDuplicates(A);
37+
38+
for (inti:A) {
39+
System.out.print(i +" ");
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp