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"));
}
}
G面经Prepare: Longest All One Substring的更多相关文章
- LeetCode 1156. Swap For Longest Repeated Character Substring
原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a str ...
- 【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 ...
- Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- G 面经 && Leetcode: Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- 2Sigma OA prepare: Longest Chain
DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- 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 ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- G面经prepare: Chucked Palindrome
给定一个字符串,找出最多有多少个chunked palindrome, 正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起 ...
随机推荐
- 一条很用的MSSQL语句
select *,ActionName= stuff((select ',' + ActionName from Sys_Action_Table where ModuleId = Sys_Modul ...
- python开发环境搭建及numpy基本属性-【老鱼学numpy】
目的 本节我们将介绍如何搭建python的开发环境以及numpy的基本属性,这样可以检验我们的numpy是否安装正确了. python开发环境的搭建 工欲善其事必先利其器,我用得比较顺手的是Intel ...
- 找出数组[1...n]中第k小元素
//问题描述: 试编写一个算法,使之能够在数组L[1...n]中找出第k小的元素(即从小到大排序后处于第k个位置的元素) #include <stdio.h> // 结合快排思想,查找第5 ...
- CSS之属性操作
(1)css text 文本 文本颜色:color 颜色属性被用来设置文字的颜色 颜色是通过css最经常的指定: *十六进制值—如:#FF0000 *一个RGB值---如:RGB(255,0,0) * ...
- 转 Using Async for File Access
原文:https://msdn.microsoft.com/en-us/library/jj155757.aspx using System; using System.Collections.Gen ...
- Windows系统Git安装配置
Git的安装 Git是一个开源的分布式的版本控制软件,是Linus Torvalds 为了方便开源贡献者协同开发和管理 Linux 内核开发替代BitKe而开发的. 打开git官网的下载地址:http ...
- matplotlib 三维旋转
# 当elevation=0时,视角为沿x1负方向看,当elevation=90时,视角沿x3负方向看.# 当azimuth=0时,视角为沿x1负方向看,当azimuth=90时,视角沿x2负方向看. ...
- 织梦,dede:list和dede:artlist的区别
dede:list可以配合pagelist进行分页,而artlsit不能进行分页. 如果要翻页只能用list的
- VUE iview date-picker取时间范围...
x HTML <script src="//unpkg.com/vue/dist/vue.js"></script> <script src=&quo ...
- 正版STLINK使用注意
原文:https://blog.csdn.net/xinghuanmeiying/article/details/78026561 盗版的TVCC是3.3v,可以只用1,7,9,12 正版的TVCC是 ...