HHR的小站
享受代码带来的快乐吧
首页
LeetCode 485. 最大连续1的个数
2020-02-05 |HHR | 代码使人快乐, LeetCode

给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:

输入: [1,1,0,1,1,1] 
输出: 3 
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 

注意:

  • 输入的数组只包含 0 和1
  • 输入数组的长度是正整数,且不超过 10,000。

class Solution {
public:
    static int findMaxConsecutiveOnes(vector<int> &nums) {
        int max = 0, num = 0;
        for (int i : nums) {
            if (i == 1)
                num++;
            else
                num = 0;
            if (max < num)
                max = num;
        }
        return max;
    }
};

https://leetcode-cn.com/problems/max-consecutive-ones/

respond-post-130

添加新评论

请填写称呼
请填写合法的电子邮箱地址
请填写合法的网站地址
请填写内容