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 ...
随机推荐
- 64、具有过渡动画效果的布局Layout( 2 )
[ CoordinatorLayout-与手势完美结合的滑动视图 ] [ AppBarLayout-可以随手势滑动的AppBar ] <android.support.design.widget ...
- Android实例-退出程序
Android实例-退出程序 http://www.cnblogs.com/FKdelphi unit Unit1; interface uses System.SysUtils, System.Ty ...
- 【BZOJ4456】[Zjoi2016]旅行者 分治+最短路
[BZOJ4456][Zjoi2016]旅行者 Description 小Y来到了一个新的城市旅行.她发现了这个城市的布局是网格状的,也就是有n条从东到西的道路和m条从南到北的道路,这些道路两两相交形 ...
- 160628、利用Oracle rownum让表排序字段值连续
利用Oracle rownum让表排序字段值连续 1.需求说明 表(eval_index)中有字段如下: 表字段 描述 说明 ID 主键 GROUP_ID 分组编号 SORT_NUM 排序序号 按照分 ...
- java上传图片,把图片存到本地
思路:js通过FileReader获取图片的Base64,Java解码用IO存到本地. HTML 代码 <input type="file" ng-model="f ...
- 阿里巴巴Java开发规约IDEA插件安装及使用
技术交流群:233513714 一.通过Jetbrains官方仓库安装 1. 打开 Settings >> Plugins >> Browse repositories.. ...
- mysql 数据库备 及移动当天数据到历史表里 window下可用
1 数据移动到历史表中,同时删除当天数据 test-move-record.bat c: cd C:\FQ_ManageServer\mysqlback mysql.exe -h 10.71.1.23 ...
- type属性对jq-post在ie、chrome、ff的兼容
w http://stackoverflow.com/questions/8834635/post-not-working-in-firefox
- 上手Keras
Keras的核心数据是“模型”,模型是一种组织网络层的方式.Keras中主要的模型是Sequential模型,Sequential是一系列网络层按顺序构成的栈. Sequential模型如下: fro ...
- SignalTap II进阶学习
1. 多级触发 有时候我们可能需要利用多个信号(最多10个)依次触发后,观察特定的值.这这时候我们可以增加触发条件来满足我们的需求. 多个触发信号如上图所示,只有依次发生SW[0]上升沿. SW[1] ...