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

 1 package GooglePhone;
 2 
 3 public class LongestOneSubstr {
 4     
 5     public static int find2(String str) {
 6         if (str==null || str.length()==0) return 0;
 7         int l = 0, r = 0;
 8         int maxLen = 0;
 9         int countZero = 0;
10         for (r=0; r<str.length(); r++) {
11             if (str.charAt(r) == '0') countZero++;
12             while (countZero > 1 && l < str.length()) {
13                 if (str.charAt(l) == '0') countZero--;
14                 l++;
15             }
16             maxLen = Math.max(r-l+1, maxLen);
17         }
18         return maxLen;
19     }
20 
21     /**
22      * @param args
23      */
24     public static void main(String[] args) {
25         // TODO Auto-generated method stub
26         System.out.println(find2("10111000010110111101101010101"));
27     }
28 
29 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6359777.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值