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

--------------------------------------------------------------------------------------------------------------------------------------

这个题可以用双指针,时间复杂度是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的更多相关文章

  1. LeetCode 485. Max Consecutive Ones (最长连续1)

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  2. 8. leetcode 485. Max Consecutive Ones

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  3. LeetCode 485 Max Consecutive Ones 解题报告

    题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...

  4. LeetCode: 485 Max Consecutive Ones(easy)

    题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...

  5. [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数

    题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...

  6. 【leetcode】485. Max Consecutive Ones

    problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...

  7. 485. Max Consecutive Ones - LeetCode

    Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...

  8. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  9. LeetCode 1004. Max Consecutive Ones III

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...

随机推荐

  1. vue路由懒加载 及import

  2. zookeeper安装以及遇到的一些坑

    最近项目中用到了storm,然后storm中用到了zookeeper,然后今天抽空整理一下zookeeper的安装使用,原来后期再慢慢学习. 本篇文档,操作部分是摘自其他博客,里边的问题分析是自己在实 ...

  3. C# 匿名对象(匿名类型)、var、动态类型 dynamic——实用之:过滤类属性、字段实用dynamic

    例子 返回一个LIst<oject>类型 而oject含有 30个字段 而我只需要两个字段.这里实用dynamic 和 linq. 上代码: 注意select new {} 为匿名类型,这 ...

  4. 解析SQL Server之任务调度

    在前面两篇文章中( 浅谈SQL Server内部运行机制 and 浅谈SQL Server数据内部表现形式 ),我们交流了一些关于SQL Server的一些术语,SQL Sever引擎 与SSMS抽象 ...

  5. C# Socket网络编程

    晚上利用空闲时间,用Socket做了一些小功能. 功能如下: a.聊天 b.传文件 c.抖动好友 主界面: 服务器 客户端 操作步骤: 服务器(测试环境的IP地址为:192.168.92.111,视情 ...

  6. oracle 当前年到指定年的年度范围求取

    如下面公式所示,求取2015到当前年(2018)的年度范围,当前年是由系统获取的,用到了sysdate和函数to_char,to_date. 当然,当前年也可以换成指定年份 SELECT TO_CHA ...

  7. Linux(CentOS7)压缩和解压缩war包、tar包、tar.gz包命令

    一.Linux版本 二.解压缩.tar.gz包到当前目录 tar -xzvf apache-tomcat-7.0.90.tar.gz 三.将指定文件压缩成.tar.gz包 tar -czf apach ...

  8. Ant Design Pro+Electron+electron-builder实现React应用脱离浏览器,桌面安装运行

    ant-design-pro ----> version :2.3.1 由于网上Ant Design Pro+Electron的资料太少,我就贡献一点经验   最近需要讲AntD Pro项目(以 ...

  9. MySQL 系列

    阅读目录 第一篇:初识数据库 第二篇:库操作相关 第三篇:表相关操作 第四篇:记录相关操作 第五篇:数据备份.pymysql模块 第六篇:视图.触发器.事务.存储过程.函数 第七篇:ORM框架SQLA ...

  10. MYSQL内置MYSQL数据库中你可以得到的信息

    1:help_topic  可以查看函数帮助,例如:SELECT * from help_topic WHERE name='concat' 可以查看concat函数. 2:SLOW_LOG 慢查询日 ...