forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_485.java
40 lines (38 loc) · 1.16 KB
/
_485.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.fishercoder.solutions;
public class _485 {
public static class Solution1 {
public int findMaxConsecutiveOnes(int[] nums) {
int maxLen = 0;
for (int i = 0; i < nums.length; i++) {
int currentLen = 0;
while (i < nums.length && nums[i] == 1) {
i++;
currentLen++;
}
maxLen = Math.max(maxLen, currentLen);
}
return maxLen;
}
}
public static class Solution2 {
/**
* Apply the sliding window template, i.e. two pointer technique.
*/
public int findMaxConsecutiveOnes(int[] nums) {
int left = 0;
int right = 0;
int ans = 0;
while (right < nums.length) {
while (right < nums.length && nums[right] == 1) {
right++;
}
ans = Math.max(ans, right - left);
while (right < nums.length && nums[right] != 1) {
right++;
}
left = right;
}
return ans;
}
}
}