leetcode解题报告(14):Max Consecutive Ones
2024-08-27 11:37:34
描述
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
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.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
分析
太简单了,一分钟ac
代码如下:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
if(nums.size() == 0)return 0;
int max = 0;
int length = 0;
for(int i = 0; i != nums.size(); ++i){
if(nums[i] == 1){
++length;
max = length > max ? length : max;
}else{
length = 0;
}
}
return max;
}
};
最新文章
- Revert R12.1.3 Homepage Layout to Link Style as in R12.1.1 or 11i
- [BZOJ2730][HNOI2012]矿场搭建(求割点)
- h5交互元素details标签
- Codeforces Beta Round #95 (Div. 2) D.Subway
- WordPress搬家教程:换空间与换域名
- POSIX线程
- Asp.net MVC23 使用Areas功能的常见错误
- web和winform的MD5加密
- apt系统中sources.list文件的解析
- oracle数据库删除数据Delete语句和Truncate语句的对比
- WebService之CXF注解报错(一)
- RabbitMQ消息队列(二):”Hello, World“
- codeforces 1153 D
- SQL CREATE DATABASE 语句
- Cordova入门系列(四)自定义Cordova插件--showToast
- 用junit对java代码进行测试,需要注意
- Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)
- QT,QT SDK, QT Creator 区别
- centos配置ssh免密码登录
- Linux 基础教程 43-su和sudo命令