原题
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input: [
1,
1,
0,
1,
1,
1]
Output:
3
Explanation: The
first two digits
or the last three digits are consecutive
1s.
The maximum
number of consecutive
1s is
3.
代码实现
public int FindMaxConsecutiveOnes(
int[] nums) {
int ito1=
0;
int max =
0;
foreach(
var item
in nums){
if(item==
1){
ito1++;
if(max<ito1)
max = ito1;
}
else
ito1=
0;
}
return max;
}
leetcode-solution库
leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp