• 作者: 负雪明烛
  • id: fuxuemingzhu
  • 个人博客:http://fuxuemingzhu.cn/

目录

  • 题目描述
  • 题目大意
  • 解题方法
    • 动态规划
  • 日期

题目地址:https://leetcode-cn.com/problems/max-consecutive-ones-ii/

题目描述

Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0.

Example 1:

Input: [1,0,1,1,0]
Output: 4
Explanation: Flip the first zero will get the the maximum number of consecutive 1s.After flipping, the maximum number of consecutive 1s is 4.

Note:

  1. The input array will only contain 0 and 1.
  2. The length of input array is a positive integer and will not exceed 10,000

Follow up:
What if the input numbers come in one by one as an infinite stream? In other words, you can’t store all numbers coming from the stream as it’s too large to hold in memory. Could you solve it efficiently?

题目大意

最多可以把数组中的一个0翻转成1,求能够成的最长连续1有多少。

解题方法

动态规划

我第一遍做这个题的时候,使用的是从左向右和从右向左两次遍历,找出每个0的左右两部分连续1的个数相加。这样也能过,但是有点麻烦。

比较好的解决方案是动态规划,定义两个变量left, right。

  1. left的含义是:遇到了0,并翻转了该0时,包含该0的位置和其左侧连续的1的的长度。
  2. right的含义是:从上次遇到0之后,累加得到的连续1的个数。

举例说明:

1 0 1 1 0
left start left end right start right end, i

维护两个变量:

  1. 当遇到1时,用right保存已经遇到的连续的1(不含翻转0得到的1)。
  2. 当遇到0时,把这个0翻转,更新left为right + 1(把位置的0翻转为1),更新right为0。

left+right的最大值即为所求。

C++代码如下:

class Solution {public:int findMaxConsecutiveOnes(vector<int>& nums) {const int N = nums.size();if (N == 0) return 0;int left = 0, right = 0;int res = 0;for (int i = 0; i < N; ++i) {if (nums[i] == 1) {right++;} else {left = right + 1;right = 0;}res = max(res, left + right);}return res;}
};

参考资料:https://www.cnblogs.com/grandyang/p/6376115.html

日期

2019 年 9 月 21 日 —— 莫生气,我若气病谁如意

【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)相关推荐

  1. Leetcode日练笔记19 #487 Max Consecutive Ones II (Medium)

    #487 Max Consecutive Ones II (Medium) Given a binary array nums, return the maximum number of consec ...

  2. 【Leetcode】487. Max Consecutive Ones II

    题目地址: https://leetcode.com/problems/max-consecutive-ones-ii/description/ 给定一个长nnn的010101数组AAA,允许将任意一 ...

  3. 【LeetCode】House Robber I II 解题报告

    [题目] I You are a professional robber planning to rob houses along a street. Each house has a certain ...

  4. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

  5. LeetCode第45场双周赛-解题报告

    LeetCode第45场双周赛-解题报告 A. 唯一元素的和 原题链接 https://leetcode-cn.com/problems/sum-of-unique-elements/ 解题思路 因为 ...

  6. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  7. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

  8. [leetcode] 213. House Robber II 解题报告

    题目链接:https://leetcode.com/problems/house-robber-ii/ Note: This is an extension of House Robber. Afte ...

  9. LeetCode 922 Sort Array By Parity II 解题报告

    题目要求 Given an array A of non-negative integers, half of the integers in A are odd, and half of the i ...

最新文章

  1. 国自然基金标书构思及撰写经验分享会
  2. 简洁版利用Python写俄罗斯方块游戏
  3. 驰骋工作流引擎设计系列05 启动流程设计
  4. TableAdapter 概述
  5. 分布式事务中间件Fescar—全局写排它锁解读
  6. hdu5106 小于x的数(二进制1确定的数)的和 数位dp(first mine)
  7. 的ppt_PPT模板中国风PPT模板
  8. C++ 中的mutable关键字
  9. 14.看板方法---运营回顾
  10. 【.md格式文件编辑器】几款主流好用的markdown编辑器介绍
  11. 大数据物流项目:概述及Docker入门(一)
  12. win10 删除右键显卡菜单项
  13. python多久可以入门_python自学要多久能学会
  14. Ruby-from百度百科
  15. 从VirtualDom(虚拟Dom)到真实DOM
  16. 一键生成舞曲编排的小程序(perl)
  17. Android视频编辑器(一)通过OpenGL预览、录制视频以及断点续录等
  18. lazada卖家如何正确做好产品定价?
  19. VC操作Excel之基本操作
  20. 车拍条件下交通标志实时识别

热门文章

  1. PHP程序测试表模板,PHPUnit 手册
  2. In despair
  3. leakcanary1.5源码分析
  4. 坐标变换的艺术—PMSM(两相)静止轴系的扩展反电势公式推导
  5. Java实现五子棋对战小游戏【完整版】
  6. C++学习笔记(一) 基础语法 —参考阿发你好
  7. mysql查找表中员工姓名性别_SQL 常见面试题解析
  8. 瓴盛科技首款AIoT产品发布,多方资本助力撬动万亿移动通信及物联网半导体市场
  9. 八数码难题 (IDA*解法)
  10. 大型电商网站系统架构