485. Max Consecutive Ones

来源:互联网 发布:农村淘宝发展现状分析 编辑:程序博客网 时间:2024/04/23 21:17

原题

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

Input: [1,1,0,1,1,1]Output: 3Explanation: 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

2 0