Given a binary array nums, return the maximum number of consecutive 1’s in the array.
示例
输入:[1,1,0,1,1,1]
输出:3
解释:开头的两位和最后的三位都是连续 1 ,所以最大连续 1 的个数是 3.
提示:
输入的数组只包含 0 和 1 。
输入数组的长度是正整数,且不超过 10,000。
解题
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int max = 0;
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) count++;
else {
max = Math.max(max, count);
count = 0;
}
}
return Math.max(max,count);
}
}
本篇文章来源于微信公众号:程序IT圈
原创文章,作者:栈长,如若转载,请注明出处:https://www.cxyquan.com/17817.html