码迷,mamicode.com
首页 > 其他好文 > 详细

485 Max Consecutive Ones 最大连续1的个数

时间:2018-04-21 22:29:03      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:连续   problem   tps   最大连续   包含   注意   for   ble   超过   

给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
    输入的数组只包含 0 和1。
    输入数组的长度是正整数,且不超过 10,000。
详见:https://leetcode.com/problems/max-consecutive-ones/description/

C++:

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int cnt=0,res=0;
        for(int num:nums)
        {
            cnt=num==0?0:cnt+1;
            res=max(res,cnt);
        }
        return res;
    }
};

 

485 Max Consecutive Ones 最大连续1的个数

标签:连续   problem   tps   最大连续   包含   注意   for   ble   超过   

原文地址:https://www.cnblogs.com/xidian2014/p/8903944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!