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. Linux之Nginx服务 nfs文件存储 负载均衡

    一.搭建Nginx服务 Nginx 是俄罗斯人编写的十分轻量级的HTTP 服务器,Nginx,它的发音为"engine X",是一个高性能的HTTP和反向代理服务器,同时也是一个I ...

  2. intellij IDEA版本控制设置

    我们开发肯定是有版本控制的,大家以前Eclipse的时候在本地文件和版本库不一致的时候,那么文件以及所在的文件夹都会出现一个〉表示,大家能很轻松的看到本地文件修改了哪一些,但是IntelliJ中默认是 ...

  3. CeontOS6.5安装php环境

    港湾云主机重装操作系统之后xshell无法连接:重启ssh:# service sshd restart -bash: vim: command not found:输入 rpm -qa|grep v ...

  4. Vue之父子组件的通信

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 洛谷 P2668 斗地主

    毒瘤题目,搞了三天-- 也没什么好讲的,就是纯搜索,先搜顺子,再搜其他的,最后单张牌和对子的时候,就不要搜索了,直接枚举,不然会T飞掉多么痛的领悟-- 主要还是靠码力 #include<iost ...

  6. Bootstrap图片支持响应式

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  7. 表单input中提示文字value随鼠标焦点移进移出而显示或隐藏的

    jQuery代码 <input value="请输入用户名" type="text"> <input value="请输入密码&qu ...

  8. mycat中间件安装与使用

    前提: 安装JDK版本在7.0及其以上 1.下载: 下载地址在:http://dl.mycat.io/ 选择1.6-release版本下载 2.安装: 直接解压即可: tar -zxf Mycat-s ...

  9. Linux基础学习-使用iSCSI服务部署网络存储

    使用iSCSI服务部署网络存储 iSCSI技术实现了物理硬盘设备与TCP/IP网络协议的相互结合,使得用户可以通过互联网方便地访问远程机房提供的共享存储资源.下面介绍如何在Linux上部署iSCSI服 ...

  10. mysql 审核引擎 goInception 的基本使用

    官网地址 github.com 安装 git clone https://github.com/hanchuanchuan/goInception.git cd goInception 修改配置 开启 ...