487. 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:
- The input array will only contain
0and1. - 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?
本题我开始用了一个方法跑了出来,但是对于one变成了多个的话就解决不了了,后来看了答案,发现可以用zero来表示出现zero的个数,然后while zero出现的次数大于k的时候把后面的指针往前移。代码如下:
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int hi = 0;
int lo = 0;
int zero = 0;
int len = 0;
while(hi<nums.length){
if(nums[hi]==0){
zero++;
}
while(zero>1){
if(nums[lo++]==0) zero--;
}
len = Math.max(len,hi-lo+1);
hi++;
}
return len;
}
}
对于出现的follow up,可以使用一个queue来做,其他的方法还是上面的方法,代码如下:
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
Queue<Integer> q = new LinkedList<Integer>();
int hi = 0;
int lo = 0;
int len = 0;
for(;hi<nums.length;hi++){
if(nums[hi]==0){
q.offer(hi);
}
while(q.size()>1){
lo = q.poll()+1;
}
len = Math.max(len,hi-lo+1);
}
return len;
}
}
487. Max Consecutive Ones II的更多相关文章
- 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [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 ...
- Leetcode: Max Consecutive Ones II(unsolved locked problem)
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
- LeetCode Max Consecutive Ones II
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- LeetCode 1004. Max Consecutive Ones III
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...
- hdoj 1977 Consecutive sum II
Consecutive sum II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- LeetCode——Max Consecutive Ones
LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...
随机推荐
- bxslider 使用帮助
“bxSlider”就是一款响应式的幻灯片js插件 bxSlider特性 充分响应各种设备,适应各种屏幕: 支持多种滑动模式,水平.垂直以及淡入淡出效果: 支持图片.视频以及任意html内容: 支持触 ...
- Mac如何让调整窗口大小更简单
在使用Mac的时候,你能把鼠标的光标悬停在任何程序的边缘,当光标自动变成箭头样式后,按住鼠标左键你将能随意拖动来改变程序窗口的大小.但是,这里有个问题,我们有时候很难控制把鼠标光标移动在正确的窗口边缘 ...
- C语言中最常用标准库函数
标准头文件包括: <asset.h> <ctype.h> <errno.h> <float.h> <limits ...
- 控制器生命周期方法(LifeCycle)
1.init方法: 在init方法中实例化必要的对象(遵从LazyLoad思想) init方法中初始化ViewController本身 2.loadView方法: 当view需要被展示而它 ...
- WebAssembly MDN简单使用
MDN 就是通过编译器编译完成c后生成的胶水代码 引入js 就能直接调用定义在c或者c++中的函数了 c代码如下: #include <stdio.h> #include <stdl ...
- centOS下jenkins
转:centos7搭建jenkins小记 转自:https://segmentfault.com/a/1190000007086764 安装java环境 1.查看服务器版本 centos7,继续. c ...
- 我的Python分析成长之路3
一 集合 ...
- module_param
该宏定义在include/linux/moduleparam.h中 #define ___module_cat(a,b) __mod_ ## a ## b #define __module_cat(a ...
- HDU 4089 && UVa 1498 Activation 带环的概率DP
要在HDU上交的话,要用滚动数组优化一下空间. 这道题想了很久,也算是想明白了,就好好写一下吧. P1:激活游戏失败,再次尝试. P2:连接失服务器败,从队首排到队尾. P3:激活游戏成功,队首的人出 ...
- 虚拟机里linux系统安装 CentOS 64-bit(6.4版本)——笔记
使用的虚拟机是VMware WorkStation 9.0(9.0.0 build-812388) 1. 安装过程中 选择 桥接 此系统可以拥有独立ip.Nat模式跟主机ip一样 2. 安装过程中选择 ...