(双指针) leetcode 485. Max Consecutive Ones
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
0and1. - The length of input array is a positive integer and will not exceed 10,000
--------------------------------------------------------------------------------------------------------------------------------------
这个题可以用双指针,时间复杂度是O(n),空间复杂度是O(1)。建立一个快指针和慢指针。
C++代码1:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
if(nums.size() == )
return ;
int maxSum = ;
int sum = ;
for(int i = ; i < nums.size(); i++){
if(nums[i] == ){
sum++;
}
else{
maxSum = max(sum,maxSum);
sum = ;
}
}
//这个不能漏掉
if(sum > maxSum)
maxSum = sum;
return maxSum;
}
};
C++代码2:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
if(nums.size() == )
return ;
int maxSum = ;
int sum = ;
for(int i = ; i < nums.size(); i++){
if(nums[i] == ){
sum++;
}
else{
sum = ;
}
maxSum = max(sum,maxSum);
}
return maxSum;
}
};
(双指针) leetcode 485. Max Consecutive Ones的更多相关文章
- LeetCode 485. Max Consecutive Ones (最长连续1)
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 8. leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode 485 Max Consecutive Ones 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
- LeetCode: 485 Max Consecutive Ones(easy)
题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
- [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数
题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...
- 【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 ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- LeetCode 1004. Max Consecutive Ones III
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...
随机推荐
- Linux硬盘文件分析取证(SSH过的IP)
在线靶场: https://www.mozhe.cn 背景介绍 某运维人员发现服务器最近被一个IP连接过SSH,请找到连接服务器SSH的IP. 实训目标 1.了解Linux备份方式: 2.了解Acce ...
- Parcelable 小记
Parcelable 类,接口类,用于数据的序列化封装.常见的Bundle,Intent类都实现了该类. 实现该类需要实现writeToParcel和describeContents方法,最后还需 ...
- 去掉所有的html标签,得到HTML标签中的所有内容
text——含有HTML标签的字符串 var text1=text.replace(/<\/?.+?>/g,""); text=text1.replace(/ /g,& ...
- Python函数的装饰器修复技术(@wraps)
@wraps 函数的装饰器修复技术,可使被装饰的函数在增加了新功能的前提下,不改变原函数名称,还继续使用原函数的注释内容: 方便了上下文环境中不去更改原来使用的函数地方的函数名: 使用方法: from ...
- Angular CLI 升级 6.0 之后遇到的问题
Angular CLI 1.7.4 在使用 ng build --prod 会构建失败,而 ng build 是正常的.比较好的解决办法是使用 ng build --prod --extract-li ...
- Neutron vxlan network
OpenStack 还支持 vxlan 和 gre 这两种 overlay network. overlay network 是指建立在其他网络上的网络. 该网络中的节点可以看作通过虚拟(或逻辑) ...
- Photoshop给人像加上个性裂纹肌肤
1.打开人物及纹理素材图片,把素材图片拖到人物图片里面,适当降低图层不透明度. 2.选择菜单:编辑 > 变形 > 自由变形,使纹理位置合适,然后确定. 3.用橡皮工具擦除多余的地方. 4. ...
- Python百题计划
一.基础篇 想要像类似执行shell脚本一样执行Python脚本,需要在py文件开头加上什么?KEY:#!/usr/bin/env python Python解释器在加载 .py 文件中的代码时,会对 ...
- java学习——递归
/** * 添加商品类型的功能 * 注意创建时间和修改时间在具体的方法中直接赋值 * @param gT 商品类型管理表映射的GT类的实例化对象 */ @Override public void ad ...
- 能ping通虚拟机中的Ubuntu,使用XShell连不上
1.在宿主机上telnet 虚拟机ip 22如果显示端口无法接通,说明你的/etc/init.d/sshd 是stop或者是异常的. 2.如果没有sshd服务,使用" sudo apt-g ...