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

Commit2d0a81b

Browse files
add a solution for 485
1 parent263e0d8 commit2d0a81b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_485.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,26 @@ public int findMaxConsecutiveOnes(int[] nums) {
1515
returnmaxLen;
1616
}
1717
}
18+
19+
publicstaticclassSolution2 {
20+
/**
21+
* Apply the sliding window template, i.e. two pointer technique.
22+
*/
23+
publicintfindMaxConsecutiveOnes(int[]nums) {
24+
intleft =0;
25+
intright =0;
26+
intans =0;
27+
while (right <nums.length) {
28+
while (right <nums.length &&nums[right] ==1) {
29+
right++;
30+
}
31+
ans =Math.max(ans,right -left);
32+
while (right <nums.length &&nums[right] !=1) {
33+
right++;
34+
}
35+
left =right;
36+
}
37+
returnans;
38+
}
39+
}
1840
}

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

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

99
publicclass_485Test {
1010
privatestatic_485.Solution1solution1;
11+
privatestatic_485.Solution2solution2;
1112

1213
@BeforeClass
1314
publicstaticvoidsetup() {
1415
solution1 =new_485.Solution1();
16+
solution2 =new_485.Solution2();
1517
}
1618

1719
@Test
1820
publicvoidtest1() {
1921
assertEquals(3,solution1.findMaxConsecutiveOnes(newint[]{1,1,0,1,1,1}));
22+
assertEquals(3,solution2.findMaxConsecutiveOnes(newint[]{1,1,0,1,1,1}));
2023
}
2124

2225
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp