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 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?

本题我开始用了一个方法跑了出来,但是对于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的更多相关文章

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

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

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

  3. 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 ...

  4. LeetCode Max Consecutive Ones II

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

  5. 485. Max Consecutive Ones最长的连续1的个数

    [抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...

  6. LeetCode 1004. Max Consecutive Ones III

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

  7. hdoj 1977 Consecutive sum II

    Consecutive sum II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. 【leetcode】485. Max Consecutive Ones

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

  9. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

随机推荐

  1. WinForm各种关闭

    Appication.Exit(); Environment.Exit(); System.Threading.Thread.CurrentThread.Abort(); Process.GetCur ...

  2. webAssmebly实现js数组排序 使用custom elements和Shadow DOM自定义组件

    直接上码了……………… .wat源码 (module (type $t0 (func (param i32 i32))) (type $t1 (func (result i32))) (type $t ...

  3. webpack执行命令的不同方式

    如使用webpack3及之前的版本只需安装webpack3即可,因为之前的webpack里面集成了webpack-cli 1. 使用局部安装webpack和webpack-cli,使用package. ...

  4. Mysql 5.7在Linux上部署及远程访问

    序言:最近要和伙伴一起组队,做.NET Core项目.所以自己就租了一个阿里云服务器,并且装了Linux和MySQL.这里面我的Linux是CentOs 7. 第一步 添加Mysql Yum库 这里面 ...

  5. 【Python学习之二】装饰器

    装饰器 首先,给出装饰器的框架: def log(func): def wrapper(*args, **kw): print('call %s():' % func.__name__) return ...

  6. 译文 编写一个loader

    https://doc.webpack-china.org/contribute/writing-a-loader loader是一个导出了函数的node模块,当资源须要被这个loader所转换的时候 ...

  7. jquery如何获取手机网页触屏坐标:ontouchstart 、ontouchend、ontouchmove

    function handleTouchEvent(event) { //只跟踪一次触摸 ) { var output = document.getElementById("output&q ...

  8. day14 迭代器,生成器,函数的递归调用

    1.什么是迭代器 迭代是一个重复的过程,但是每次重复都是基于上一次重复的结果而继续 迭代取值的工具 2.为什么要用迭代器 迭代器的优点 ​ ①不依赖于索引取值 ​ ②更节省内存 缺点: ​ 1.不如按 ...

  9. Python 基本数据类型 (一) - 整数

    帮助文档网址: https://docs.python.org/3.7/tutorial/introduction.html 待补充

  10. cf 1016D

    D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...