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. Vue前端利用qrcode生成二维码

    <div id="qrcode" style="width: 560px;height: 560px;background-color: white;"& ...

  2. 2018-2019-1 20189201《Linux内核原理与分析》第三周作业

    写作业之前,写了时光博物馆参观感受.1978-2018 40年的改革开放历程. 一.C语言中内嵌汇编语言的写法 内嵌汇编的语法如下: asm volatile ( 汇编语句模版: 输出部分: 输入部分 ...

  3. 一次国际化记录以及平铺JSON数据

    ​ 写这个方法的原因是因为我们需要改版国际化,因为相同的项目有其他分支做过国际化,但是主版本没有进行过国际化,目前需要修改主版本的国际化,但是因为国际化的方式做了结构上的调整所以写了这个工具方法方便去 ...

  4. spring-boot-maven-plugin 安装本地jar 包

    本地使用nexus 进行maven仓库管理.项目deploy 引入之后,总是找不到jar中定义的class或者配置文件等. 从截图上可以看到虽然class文件是有的,但是引用的时候却是找不到的. Sp ...

  5. VS2008,System.Runtime.InteropServices.COMException (0x800401F3): Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))

    在VS2008环境中编译调试运行不报错,但在发布的exe文件运行就报错 System.Runtime.InteropServices.COMException (0x800401F3): Invali ...

  6. 《SpringMVC从入门到放肆》六、SpringMVC开发Controller的方法总结

    到目前为止我们已经大概学习了StringMVC的执行流程,以及详细的处理器映射器和处理器适配器的执行流程,并可以自己写一个配置方式开发的小Demo了.今天我们来总结一下实现一个Controller的几 ...

  7. Codechef August Challenge 2018 : Coordinate Compression

    传送门 外边二分,里面拿线段树维护贪心就行了. #include<cstdio> #include<vector> #include<cstring> #inclu ...

  8. 7、vue-awesome-swiper页面跳转

    <template> <swiper :options='swiperOption' ref="mySwiper" class='swiper-container ...

  9. larave异步多图片上传的实现和注意事项及$file的对象函数

    要使用多图片上传,首先要在input添加multipart,同时注意name的参数要加[],不然,不算是数组.具体如下,注意绿色地方.如果是单张图片,把[]去掉,不要multiple; <inp ...

  10. ubuntu安装elasticsearch

    0x00安装jdk ElasticSearch需要安装jdk1.8以上版本的支持,所以需要先安装jdk.linux下如何安装可以查看另一篇博客 0x01 下载elasticsearch 在es官网下载 ...