(双指针) 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 ...
随机推荐
- Vue2.0 --- vue-cli脚手架中全局引入JQ
第一步:安装jQuery npm/cmpn方式安装(默认安装1.7.X版本的JQ) npm/cnpm install jQuery 如果想安装更高版本的JQ那么可以选择在package.json文件下 ...
- vscode指定扩展安装位置
默认情况下,(Windows)vscode的安装路径为C:\Users\用户名\.vscode\extensions. 如果想要自定义扩展的安装路径,无法直接在vscode中修改.但是,在启动vsco ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 为什么CynosDB叫真正的云原生数据库?
本文由腾讯云数据库发表 注:本文摘自2018年11月22日腾讯云数据库CynosDB新品发布会的演讲实录.随着互联网信息的发展,大家也对云这个词汇也不是特别陌生了,作为全球首选的云服务厂商之一的腾讯云 ...
- CG-CTF simple-machine
运行一下,输入flag: 用ida打开: input_length和input_byte_804B0C0为重命名的变量:现在一个个看调用的函数. sub_8048526(): 这个函数使用了mmap分 ...
- Vue的安装及使用快速入门
一.安装vue 1.安装node.js,安装完node.js之后,npm也会自动安装 查询是否安装成功的命令: node -v npm -v 2.全局安装脚手架工具vue-cli,命令如下: npm ...
- 基于GPS数据建立隐式马尔可夫模型预测目的地
<Trip destination prediction based on multi-day GPS data>是一篇在2019年,由吉林交通大学团队发表在elsevier期刊上的一篇论 ...
- Azure导出所有用户权限---powershell命令
直接运行脚本 #requires -Version 3.0 -Modules AzureRM.Resourcesparam( [switch] $GroupRolesB ...
- MYSQL内置MYSQL数据库中你可以得到的信息
1:help_topic 可以查看函数帮助,例如:SELECT * from help_topic WHERE name='concat' 可以查看concat函数. 2:SLOW_LOG 慢查询日 ...
- Vue.Draggable 文档总结
本文章转自https://blog.csdn.net/zjiang1994/article/details/79809687 Vue.Draggable学习总结 Draggable为基于Sortabl ...