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

Commite547fc1

Browse files
add a solution for 26
1 parentfbffcce commite547fc1

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed
Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
packagecom.fishercoder.solutions;
22

3+
importcom.fishercoder.common.utils.CommonUtils;
4+
35
publicclass_26 {
46

5-
publicstaticclassSolution1 {
6-
/**Key: It doesn't matter what you leave beyond the returned length.*/
7-
publicintremoveDuplicates(int[]nums) {
8-
inti =0;
9-
for (intj =1;j <nums.length;j++) {
10-
if (nums[i] !=nums[j]) {
11-
i++;
12-
nums[i] =nums[j];
7+
publicstaticclassSolution1 {
8+
/**
9+
* Key: It doesn't matter what you leave beyond the returned length.
10+
*/
11+
publicintremoveDuplicates(int[]nums) {
12+
inti =0;
13+
for (intj =1;j <nums.length;j++) {
14+
if (nums[i] !=nums[j]) {
15+
i++;
16+
nums[i] =nums[j];
17+
}
18+
}
19+
returni +1;
1320
}
14-
}
15-
returni +1;
1621
}
17-
}
22+
23+
publicstaticclassSolution2 {
24+
/**
25+
* My completely original solution on 2/2/2022.
26+
*/
27+
publicintremoveDuplicates(int[]nums) {
28+
intleft =0;
29+
intright =1;
30+
for (;right <nums.length;right++) {
31+
while (right <nums.length &&nums[right] ==nums[right -1]) {
32+
right++;
33+
}
34+
if (right <nums.length) {
35+
nums[++left] =nums[right];
36+
}
37+
}
38+
CommonUtils.printArray(nums);
39+
returnleft +1;
40+
}
41+
}
42+
1843
}

‎src/test/java/com/fishercoder/_26Test.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88

99
publicclass_26Test {
1010
privatestatic_26.Solution1solution1;
11+
privatestatic_26.Solution2solution2;
1112
privatestaticint[]nums;
1213

1314
@BeforeClass
1415
publicstaticvoidsetup() {
1516
solution1 =new_26.Solution1();
17+
solution2 =new_26.Solution2();
1618
}
1719

1820
@Test
1921
publicvoidtest1() {
2022
nums =newint[]{1,1,2};
2123
assertEquals(2,solution1.removeDuplicates(nums));
24+
assertEquals(2,solution2.removeDuplicates(nums));
2225
}
2326

2427
@Test

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp