give a string, all 1 or 0, we can flip a 0 to 1, find the longest 1 substring after the flipping

这是一个简单版本of LC 424 Longest Repeating Character Replacement

又是Window, 又是Two Pointers

Window还是采用每次都try to update left使得window valid, 每次都检查最大window

 package GooglePhone;

 public class LongestOneSubstr {

     public static int find2(String str) {
if (str==null || str.length()==0) return 0;
int l = 0, r = 0;
int maxLen = 0;
int countZero = 0;
for (r=0; r<str.length(); r++) {
if (str.charAt(r) == '0') countZero++;
while (countZero > 1 && l < str.length()) {
if (str.charAt(l) == '0') countZero--;
l++;
}
maxLen = Math.max(r-l+1, maxLen);
}
return maxLen;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(find2("10111000010110111101101010101"));
} }

G面经Prepare: Longest All One Substring的更多相关文章

  1. LeetCode 1156. Swap For Longest Repeated Character Substring

    原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a str ...

  2. 【leetcode】1156. Swap For Longest Repeated Character Substring

    题目如下: Given a string text, we are allowed to swap two of the characters in the string. Find the leng ...

  3. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  4. G 面经 && Leetcode: Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  5. 2Sigma OA prepare: Longest Chain

    DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...

  6. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  7. G面经prepare: Set Intersection && Set Difference

    求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...

  8. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  9. G面经prepare: Chucked Palindrome

    给定一个字符串,找出最多有多少个chunked palindrome, 正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起 ...

随机推荐

  1. python全栈开发day102-django rest-framework框架

    1.频次访问组件 1) 手写版本 # class VisitThrottle(BaseThrottle): # # def __init__(self): # self.history = None ...

  2. 在XPS13 上安装Ubuntu 16.04

    1 准备系统安装U盘 使用常见的光盘工具软件ultraISO. (1)首先使用UltraISO打开Ubuntu-16.04.4-desktop-amd64.iso安装映像. (2)在菜单栏中,选择&q ...

  3. SQL反模式学习笔记8 多列属性

    目标:存储多值属性 反模式:创建多个列.比如一个人具有多个电话号码.座机号码.手机号码等. 1.查询:多个列的话,查询时可能不得不用IN,或者多个OR: 2.添加.删除时确保唯一性.判断是否有值:这些 ...

  4. 原生HttpClient详细使用示例

    一.HttpClient类 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; ...

  5. 多线程处理list

    package com.zhx.web.invoice.service; import java.util.*; import java.util.concurrent.Callable; impor ...

  6. 初窥Java之六

    一.二维数组 1.动态创建 数组中元素类型[] 数组名 = new 数组中元素类型[]; 例如:int[][] arr = new int[二维数组的长度][一维数组的长度]: Int[][] arr ...

  7. python yield 和 yield from用法总结

    #例1. 简单输出斐波那契數列前 N 个数#缺点:该函数可复用性较差,因为 fab 函数返回 None,其他函数无法获得该函数生成的数列#要提高 fab 函数的可复用性,最好不要直接打印出数列,而是返 ...

  8. JavaScript中作用域和作用域链的简单理解(变量提升)

    通过阅读<JS高级程序设计>这本书,对js中的作用域和作用域链知识有了初步的了解和认识,准备成笔记供大家参考,笔记中字数比较多,但个人认为叙述的挺详细的,所以希望读者耐心看.再者,本人了解 ...

  9. java集合的复习

    1:自定义的linkedList链表 https://blog.csdn.net/qq_33471403/article/details/80109620 2:用linked    https://b ...

  10. Centos 7 上使用nginx为Node.js配置反向代理时错误:(13: Permission denied) while connecting to upstream

    错误来源:Centos 7 上使用nginx为Node.js配置反向代理时产生(13: Permission denied) while connecting to upstream的错误 nginx ...