1、题目描述

2、问题分析

遍历一次数组,以每个1 为起点向后数,数到0 时比较当前1的个数和最大1 的个数,然后将遍历的起点放到当前0 的后面。

3、代码

 int findMaxConsecutiveOnes(vector<int>& nums) {
int result = ;
if( nums.size() == ) return result; for( int i = ; i < nums.size() ; i++ ){
if( nums[i] == ){
int j = i;
int num_1 = ;
while( j < nums.size() && nums[j] == ){
num_1++;
j++;
i = j-;
} if( num_1 > result ){
result = num_1;
}
} } return result ; }

LeetCode题解之Max Consecutive Ones的更多相关文章

  1. 【leetcode】485. Max Consecutive Ones

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

  2. 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  3. LeetCode算法题-Max Consecutive Ones(Java实现)

    这是悦乐书的第242次更新,第255篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第109题(顺位题号是485).给定二进制数组,找到此数组中连续1的最大数量.例如: 输 ...

  4. 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  5. 【LeetCode】1004. Max Consecutive Ones III 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法/双指针 日期 题目地址:https://le ...

  6. 【leetcode】1004. Max Consecutive Ones III

    题目如下: Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of ...

  7. LeetCode Max Consecutive Ones II

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...

  8. LeetCode 1004. Max Consecutive Ones III

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

  9. [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 ...

随机推荐

  1. Redis 缓存服务配置与使用

    缓存服务器Couchbase另外一种选择Redis documentation http://redis.io/documentation http://redis.cn/documentation. ...

  2. 《第一本Docker书》

    Docker简介 Docker依赖写时复制(copy-on-write),使修改应用程序非常迅速. Docker推荐单个容器只运行一个应用或进程,鼓励面向服务的架构和微服务架构. Docker的核心组 ...

  3. javascript实现代码高亮-wangHighLighter.js

    1. 引言 (先贴出wangHighLighter.js的github地址:https://github.com/wangfupeng1988/wangHighLighter注意,程序和使用说明的更新 ...

  4. 记一次使用mybatis进行like 模糊查询遇到的问题

    "bdate like '#{date}%' and ..." 最开始这样写的· 将传入的参数和%用单引号包起来,但是这会报错 java.sql.SQLException: Par ...

  5. linux开机自启动设置,自定义开机启动模版,nginx开机自启动服务

    /etc/init.d 目录,我们把shell脚本放在这个目录下来作为启动脚本 都是用来放服务脚本的,当Linux启动时,会寻找这些目录中的服务脚本,并根据脚本的run level确定不同的启动级别. ...

  6. 使用ssh-add命令添加ssh私钥时报错

    当使用ssh-add命令添加ssh私钥时,报如下错误: Could not open a connection to your authentication agent. 其实需要先执行如下命令: e ...

  7. superset--presto sql

    1.hive的partition的相关查询,由于presto不支持vachar和int的自动转换,所以使用where的时候需要手动转换一下. #select count(*) from userlog ...

  8. [转]WordPress 主题教程 #2:模板文件和模板

    本文转自:http://blog.wpjam.com/m/wp-theme-lesson-2-template-files-and-templates/ 模板文件(template files)和模板 ...

  9. vs2017 未能完成操作。不支持此接口

    打开vs2017开发者命令提示符 切换至安装下的指定目录 执行下面的命令就可以了    需要注意的是一定要用vs2017的开发人员命令提示符  别用cmd gacutil -i Microsoft.V ...

  10. [android] fragment的生命周期和通讯

    重写一下生命周期方法 所有的fragment都是依附于activity的,只有当activity显示出来的时候,fragment才能够创建上去 onAttach,当附加到activity上的时候 on ...