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. sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码

    不论使用urllib还是使用requests库经常会遇到中文编码错误的问题,我就经常遇到,因为python安装在windows平台上,cmd的默认编码为GBK,所以在cmd中显示中文时会经常提示gbk ...

  2. Java8新特性----Stream

    Stream Stream 是用函数式编程方式在集合类上进行复杂操作的工具. 一)常用的流操作 惰性求值方法:只描述Stream,最终不产生新集合的方法(返回的还是Stream). 及早求值方法:最终 ...

  3. jupyter notebook 远程访问

    https://www.youtube.com/watch?v=LpQl0yeZzCU 在服务器端执行: jupyter notebook --ip 服务器的Ip地址 --allow-root --n ...

  4. vue 监听路由变化

    方法一:通过 watch // 监听,当路由发生变化的时候执行 watch:{ $route(to,from){ console.log(to.path); } }, 或 // 监听,当路由发生变化的 ...

  5. (68)Wangdao.com第十一天_JavaScript 数组的常用方法

    数组的常用方法: 向数组末尾添加一个或多个元素,返回新长度 var arr = new Array(); arr.push("唐僧"); // 返回 1 删除数组最后一个元素,返回 ...

  6. react_app 项目开发 (7)_难点集合

    /src/App/Admin/Header 布局 import {Row, Col} from "antd" <div className="header_box& ...

  7. centos7下NFS使用与配置

    NFS是Network File System的缩写,即网络文件系统.客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下. nfs为什么需要RPC?因为NFS支持的功能很多,不同功能会 ...

  8. poj 3422 最小费用流

    如果不是从费用流区做这个题几乎不会想到用费用流 点有权值很容易想到拆点 问题是求最大sum ...  把权值取负 这样最小费用流的相反数就是最大sum 源点S汇点T k为移动次数 矩阵中的点拆成入点出 ...

  9. 使用Eclipse+jlink调试STM32

    使用Eclipse+JLINK调试STM32 安装eclipse + CDT. 安装交叉编译工具(工具链ARM CROSS GCC--GUN ARM http://gnuarmeclipse.sour ...

  10. springboot+rediscluster

    @EnableCaching @Configuration public class RedisConfiguration extends CachingConfigurerSupport { @Au ...