首页 > 学院 > 开发设计 > 正文

485. Max Consecutive Ones

2019-11-08 02:57:44
字体:
来源:转载
供稿:网友

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.

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.

class Solution {public: int findMaxConsecutiveOnes(vector<int>& nums) { int nTimes = 0, maxOne = 0; int sz = nums.size(); for(int i = 0; i < sz; ++i){ if(nums[i] == 1){ ++nTimes; if(nTimes > maxOne) maxOne = nTimes; } else nTimes = 0; } return maxOne; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表