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

Commit623c431

Browse files
fix 487
1 parentebe3226 commit623c431

File tree

3 files changed

+140
-27
lines changed

3 files changed

+140
-27
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ _If you like this project, please leave me a star._ ★
787787
|491|[Increasing Subsequences](https://leetcode.com/problems/increasing-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_491.java) | |Medium| Backtracking, DFS
788788
|490|[The Maze](https://leetcode.com/problems/the-maze/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_490.java) | |Medium| BFS
789789
|488|[Zuma Game](https://leetcode.com/problems/zuma-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_488.java) | |Hard | DFS, Backtracking
790-
|487|[Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_487.java) |[:tv:](https://youtu.be/nKhteIRZ2Ok) |Medium| Array
790+
|487|[Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_487.java) |[:tv:](https://youtu.be/nKhteIRZ2Ok) |Medium| Array, Sliding Window
791791
|486|[Predict the Winner](https://leetcode.com/problems/predict-the-winner/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_486.java) | | Medium | DP
792792
|485|[Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/)|[Java](../master/src/main/java/com/fishercoder/solutions/_485.java)[Javascript](../master/javascript/_485.js)|[:tv:](https://youtu.be/nKhteIRZ2Ok)|Easy| Array
793793
|484|[Find Permutation](https://leetcode.com/problems/find-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_484.java) | |Medium | Array, String, Greedy

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@
22

33
publicclass_487 {
44

5-
publicstaticintfindMaxConsecutiveOnes(int[]nums) {
6-
intmaxOnes =0;
7-
for (inti =0;i <nums.length;i++) {
8-
intnewOnes =0;
9-
while (i <nums.length &&nums[i] ==1) {
10-
newOnes++;
11-
i++;
5+
publicstaticclassSolution1 {
6+
/**
7+
* I implemented this on my own after a quick read from https://leetcode.com/problems/max-consecutive-ones-ii/solution/
8+
*/
9+
publicstaticintfindMaxConsecutiveOnes(int[]nums) {
10+
intleft =0;
11+
intright =0;
12+
intzeroes =0;
13+
intans =0;
14+
while (right <nums.length) {
15+
if (nums[right] ==0) {
16+
zeroes++;
17+
}
18+
if (zeroes <=1) {
19+
ans =Math.max(ans,right -left +1);
20+
}else {
21+
while (left <nums.length &&zeroes >1) {
22+
if (nums[left] ==0) {
23+
zeroes--;
24+
}
25+
left++;
26+
}
27+
}
28+
right++;
1229
}
13-
maxOnes =Math.max(maxOnes,newOnes);
30+
returnans;
1431
}
15-
returnmaxOnes;
1632
}
17-
1833
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp