LeetCode: 485 Max Consecutive Ones(easy)
2024-08-30 11:49:11
题目:
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
and1
. - The length of input array is a positive integer and will not exceed 10,000
代码:
别人的:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int count = , max = ;
for (int i = ; i < nums.size(); i++) {
if (nums[i] == && (!i || nums[i - ] != )) count = ;
else if (i && nums[i] == && nums[i - ] == ) count++;
if (max < count) max = count;
}
if (max < count) max = count;
return max;
}
};
自己的:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int result = ;
int tem = ;
nums.push_back();
for (auto c : nums){
if(c == ){
if ( tem > result)
result = tem;
tem = ;
}
else
tem++;
}
return result;
}
};
最新文章
- 使用批处理文件在FTP服务器 上传下载文件
- Dubbo架构设计详解
- JVM 类型的生命周期学习
- 多语言文本资源的访问(Windows:ini)
- 【Android】 分享一个完整的项目,适合新手!
- CSS预处理语言——less与sass的使用
- 进阶-MongoDB 知识梳理
- 4.app是怎么炼成的
- day 05字典相关内容
- spring 动态代理
- C#中List按特定字段排序
- 【转】WPS word 文档中的插入对象 为什么打不开
- Python-CSS高级 题目
- Appium入门(4)__ Appium Client安装
- Eclipse如何导入DemoWeb.rar
- java RE Validation常用
- sourcetree回退已推送的代码
- node 跨域
- CAS-登出配置
- 从数据库表导出为excel表格