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

[LeetCode]485. Max Consecutive Ones

2019-11-06 06:36:57
字体:
来源:转载
供稿:网友

[LeetCode]485. Max Consecutive Ones

题目描述

这里写图片描述

思路

遍历数组 如果为1,计数加 如果为0或数组最后一位,max更新,计数重置

代码

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