[leetCode]485. 最大连续1的个数
作者:互联网
题目
https://leetcode-cn.com/problems/max-consecutive-ones/
滑动窗口
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int len = nums.length;
int left = 0;
int right = 0;
int maxOneCnt = 0;
while (right < len) {
int num = nums[right++];
if (num == 1) {
maxOneCnt = Math.max(right - left, maxOneCnt);
} else {
left = right;
}
}
return maxOneCnt;
}
}
一次遍历
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int len = nums.length;
int count = 0;
int maxCount = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == 1) {
count++;
} else {
maxCount = Math.max(maxCount, count);
count = 0;
}
}
maxCount = Math.max(maxCount, count);
return maxCount;
}
}
标签:count,right,nums,int,个数,maxOneCnt,maxCount,485,leetCode 来源: https://blog.csdn.net/renweiyi1487/article/details/113813381