题目:

思路:


代码:
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int count = 0;
int maxCount = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 1) {
// Increment the count of 1's by one.
count += 1;
} else {
// Find the maximum till now.
maxCount = Math.max(maxCount, count);
// Reset count of 1.
count = 0;
}
}
return Math.max(maxCount, count);
}
}
本文介绍了一个寻找整数数组中最大连续1序列长度的算法。通过遍历数组并使用计数器跟踪连续1的数量,该算法能高效地找到最长的连续1序列。最后返回最长连续1序列的长度。
750

被折叠的 条评论
为什么被折叠?



