G面经Prepare: Longest All One Substring

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"));
} }
上一篇:L2 L3 L4


下一篇:Scala快速入门到精通 视频教程 百度云网盘下载地址