We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4eb2a5e commit 1ab0ff1Copy full SHA for 1ab0ff1
Easy/Max Consecutive Ones.java
@@ -1,20 +1,16 @@
1
class Solution {
2
- public int findMaxConsecutiveOnes(int[] nums) {
3
- int i = 0;
4
- int maxOnes = Integer.MIN_VALUE;
5
- int temp = 0;
6
- while (i < nums.length) {
7
- if (nums[i] == 1) {
8
- temp++;
9
- }
10
- else {
11
- maxOnes = Math.max(maxOnes, temp);
12
- temp = 0;
13
14
-
15
- i++;
+ public int findMaxConsecutiveOnes(int[] nums) {
+ int maxCount = 0;
+ for (int i = 0; i < nums.length; ) {
+ if (nums[i] == 1) {
+ int currIdx = i;
+ while (i < nums.length && nums[i] == 1) {
+ i++;
16
}
17
18
- return Math.max(maxOnes, temp);
+ maxCount = Math.max(maxCount, i - currIdx);
+ }
19
+ return maxCount;
20
0 commit comments