【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/max-consecutive-ones-ii/
题目描述
Given a binary array, find the maximum number of consecutive 1
s 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:
- The input array will only contain 0 and 1.
- 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。
- left的含义是:遇到了0,并翻转了该0时,包含该0的位置和其左侧连续的1的的长度。
- right的含义是:从上次遇到0之后,累加得到的连续1的个数。
举例说明:
1 | 0 | 1 | 1 | 0 |
---|---|---|---|---|
left start | left end | right start | right end, i |
维护两个变量:
- 当遇到1时,用right保存已经遇到的连续的1(不含翻转0得到的1)。
- 当遇到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++)的更多相关文章
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】1004. Max Consecutive Ones III 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法/双指针 日期 题目地址:https://le ...
- 【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学方法 日期 题目地址:https://leetc ...
- 【LeetCode】910. Smallest Range II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】45. Jump Game II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
随机推荐
- AWS EKS 添加IAM用户角色
作者:SRE运维博客 博客地址: https://www.cnsre.cn/ 文章地址:https://www.cnsre.cn/posts/211203931498/ 相关话题:https://ww ...
- Linux之sed命令常见用法
1. sed(stream editor),流编辑器 linux中,主要中sed命令实现对文件的增删改替换查 名称 sed - 用于过滤和转换文本的流编辑器 SYNOPSIS sed [选项]... ...
- 【Redis】Sentinel 哨兵模式
Sentinel(哨兵模式) 目录 Sentinel(哨兵模式) 哨兵模式的三个定时任务 Sentinel(哨兵)与Sentinel .主服务器.从服务器之间的连接 检测下线状态 选择领头 Senti ...
- linux 软链接与查看历史指令
ln 说明 软连接也叫符号链接,类似于windows里的快捷方式,主要存放了路径. 基本语法 ln -s[原文件或目录][软连接名] 删除软链接 [root@hadoop102 ~]# rm -rf ...
- C语言内自定义汇编函数&调用约定
探究如何在C语言里直接自写汇编函数 裸函数 裸函数与普通函数的区别 普通函数在经过编译器编译时,编译器自动生成保护现场,恢复现场等反汇编代码 当我们想要自己实现函数内部的汇编代码时,就可以告诉汇编器不 ...
- 用usb线配置直流电机驱动器不能配置成功
原因可能是因为usb线的问题 换了三条usb线. 这三条都是通的,用万用表测试都是通的,但是进行电机配置的时候不行. 猜测原因可能是三条usb线的芯材质不同导致压降不同,使得通信故障.
- set、multiset深度探索
set/multiset的底层是rb_tree,因此它有自动排序特性.set中的元素不允许重复必须独一无二,key与value值相同,multiset中的元素允许重复. set的模板参数key即为关键 ...
- 给webapp加上一个apk外壳
原文:http://blog.csdn.net/cmyh100/article/details/77862962 1.在Android Studio里创建一个项目 2.创建MyApplication. ...
- display:inline-block,block,inline元素的区别
1.display:block将元素显示为块级元素,从而可以更好地操控元素的宽高,以及内外边距,每一个块级元素都是从新的一行开始.2.display : inline将元素显示为行内元素,高度,行高以 ...
- js 时间戳转换为年月日时分秒的格式
<script type="text/javascript"> var strDate = ''; $(function(){ // 获取时间戳 var nowDate ...