LeetCode——Max Consecutive Ones
LeetCode——Max Consecutive Ones
Question
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
解题思路
用stack求解。
具体实现
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
stack<int> st;
int max_len = 0;
for (int i : nums) {
if (i == 0) {
if (st.size() > max_len)
max_len = int(st.size());
// 清空栈
while(!st.empty())
st.pop();
} else
st.push(i);
}
return max(int(st.size()), max_len);
}
};
用一个计数器也可以求解。
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int max=0,cur=0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]&1)
{
max=max>++cur?max:cur;
}
else cur=0;
}
return max;
}
};
时间复杂度都是O(n),但是计数器更快一些。
LeetCode——Max Consecutive Ones的更多相关文章
- [LeetCode] Max Consecutive Ones II 最大连续1的个数之二
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
- LeetCode Max Consecutive Ones II
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...
- Leetcode: Max Consecutive Ones II(unsolved locked problem)
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
- [LeetCode] Max Consecutive Ones 最大连续1的个数
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode: Max Consecutive Ones
这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...
- LeetCode 1004. Max Consecutive Ones III
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
随机推荐
- android--WaveView(波浪形View) 的实现记录
背景 请假回家当伴郎,由于实在无聊,就写下了此篇博客!!按照惯例,先上动态图 怎么样!效果比较赞吧!!! 思路 当我第一次看见这个效果的时候,我的第一个想法是:如果是静态的时候是什么样子的!好,再来张 ...
- linux jdk 6 版本下载
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-41940 ...
- git GUI 入门
一:安装一个git 及gui 二:配置gui及线上的git链接 在Git Gui中,选择Remote->add添加远程服务器,远程服务器信息有两种填写方式,填写https地址或ssh地址,对应g ...
- VMware虚拟机NAT(地址转换模式)
转载于:https://www.linuxidc.com/Linux/2016-09/135521p2.htm 二.NAT(地址转换模式) 刚刚我们说到,如果你的网络ip资源紧缺,但是你又希望你的 ...
- APP上传应用商店加固后打包
在cmd进入jdk的bin目录,把keystore文件和apk安装包放到bin目录下,然后执行以下命令,需要管理员权限: jarsgner -verbose -sigalg SHA1withRSA - ...
- mongodb3.0+ 版本内置数据压缩
mongodb3+版本之后支持zlib和snappy. 创建压缩的集合 db.createCollection( "email", {storageEngine:{wiredTig ...
- Spring容器初始化的时候如何添加一个定时器?
昨天遇到这个问题,在项目启动的时候添加一个定时器隔一段时间扫描有没有定时发送的邮件(当然也可以是你自己的业务逻辑),也在网上找了资料,加上自己的修改,终于成功了.所以来做个记录. 1.ServletC ...
- monggodb 模糊查询
MongoDB的模糊查询其实很简单: 11.LIKE模糊查询userName包含A字母的数据(%A%) SQL:SELECT * FROM UserInfo WHERE user ...
- word2007的配置进度怎么产生的?如何解决?
那么要怎么解决这个问题呢?既然是安装的,那么我们便道安装控制器文件夹下面去找原因.在WIN8操作系统下,文件夹位于:C:\Program Files (x86)\Common Files\Micros ...
- 我的Android进阶之旅------>解决Error:Unable to find method 'org.gradle.api.internal.project.ProjectInternal.g
错误描述 今天在Github上面下载了一份代码,然后导入到Android Studio中直接报了如下图所示的错误: 错误描述如下: Error: Unable to find method 'org. ...