原题
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

本文介绍了一种解决LeetCode上经典算法题的方法:如何找出二进制数组中最长的连续1序列。通过一个简单的C#代码示例展示了如何遍历数组并记录最长连续1的个数。
1109

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



