博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Max Consecutive Ones
阅读量:4361 次
发布时间:2019-06-07

本文共 780 字,大约阅读时间需要 2 分钟。

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.

Note:

  • The input array will only contain 0 and 1.
  • The length of input array is a positive integer and will not exceed 10,000

题目要求连续1的个数。遍历数组进行判断取最大的连续1的个数即可。

class Solution {public:    int findMaxConsecutiveOnes(vector
& nums) { int res = 0, cnt = 0; for (int i = 0; i != nums.size(); i++) { if (nums[i] == 1) cnt++; else cnt = 0; res = max(res, cnt); } return res; }};// 36 ms

 

转载于:https://www.cnblogs.com/immjc/p/7146570.html

你可能感兴趣的文章
Jdk与Tomcat配置与安装
查看>>
关于一个Java web与JFrame的深度结合
查看>>
VB连数据库conn.open的参数
查看>>
《信息安全系统设计基础》实验三
查看>>
SpringBoot Docs
查看>>
解决sublime text 2总是在新窗口中打开文件(标签中打开)
查看>>
VUE AntDesign DatePicker设置默认显示当前日期
查看>>
WIN32窗口模板
查看>>
859. Buddy Strings - LeetCode
查看>>
[置顶] 关键字弹出动画
查看>>
支付宝api指南
查看>>
二叉树的广度优先遍历、深度优先遍历的递归和非递归实现方式
查看>>
docker-compose部署kafka
查看>>
IOS中NSUserDefaults的用法(轻量级本地数据存储)
查看>>
cms项目技术心得!
查看>>
Django模板系统
查看>>
位(Bit)与字节(Byte)
查看>>
关于两次指针(struct型)传参数的问题
查看>>
在Logstash的配置文件中对日志事件进行区分
查看>>
字符串之strcmp
查看>>