Java for LeetCode 076 Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
Note:
If there is no such window in S that covers all characters in T, return the emtpy string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
解题思路:
涉及到查找操作,将T放进tMap中,同时创建一个保存T中所有字符(不重复)的图sMap,用一个计数器表示sMap和tMap的重合程度,一旦sMap包含了tMap即找到了一组解,通过收缩begin指针获取刚好包含tMap的位置,之后就是比较大小了,JAVA实现如下:
public String minWindow(String s, String t) {
HashMap<Character, Integer> tMap = new HashMap<Character, Integer>();
HashMap<Character, Integer> sMap = new HashMap<Character, Integer>();
for (int i=0;i<t.length();i++){
sMap.put(t.charAt(i), 0);
if (!tMap.containsKey(t.charAt(i)))
tMap.put(t.charAt(i), 1);
else
tMap.put(t.charAt(i), tMap.get(t.charAt(i)) + 1);
}
int begin=0,count=0,minBegin=0,length=s.length()+1;;
for(int i=0;i<s.length();i++){
if(!tMap.containsKey(s.charAt(i)))
continue;
sMap.put(s.charAt(i), sMap.get(s.charAt(i))+1);
if(sMap.get(s.charAt(i))<=tMap.get(s.charAt(i)))
count++;
if(count==t.length()){
for(int j=begin;j<=i;j++){
if(!tMap.containsKey(s.charAt(j)))
continue;
if(sMap.get(s.charAt(j))>tMap.get(s.charAt(j))){
sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
continue;
}
sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
count--;
begin=j+1;
if(length>i-j){
length=i-j;
minBegin=j;
}
break;
}
}
}
return length!=s.length()+1?s.substring(minBegin, minBegin+length+1):"";
}
Java for LeetCode 076 Minimum Window Substring的更多相关文章
- [LeetCode] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [leetcode]76. Minimum Window Substring最小字符串窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【leetcode】Minimum Window Substring (hard) ★
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 076 Minimum Window Substring 最小窗口子字符串
给定一个字符串 S 和一个字符串 T,找到 S 中的最小窗口,它将包含复杂度为 O(n) 的 T 中的所有字符.示例:S = "ADOBECODEBANC"T = "AB ...
- Leetcode#76 Minimum Window Substring
原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
随机推荐
- Oracle常用sql
Oracle不像Sqlserver,并没有提供l默认约束,但提供了默认值,效果一样.--------------------------- 在建表时设置默认约束-------------------- ...
- BZOJ-2038 小Z的袜子(hose) 莫队算法
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MB Submit: 5573 Solved: 2568 [Subm ...
- 学习笔记 BIT(树状数组)
痛定思痛,打算切割数据结构,于是乎直接一发BIT 树状数组能做的题目,线段树都可以解决 反之则不能,不过树状数组优势在于编码简单和速度更快 首先了解下树状数组: 树状数组是一种操作和修改时间复杂度都是 ...
- Spring MVC实现防止表单重复提交(转)
Spring MVC拦截器+注解方式实现防止表单重复提交
- static NSString *ID的改进
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...
- DedeCMS全版本通杀SQL注入漏洞利用代码
EXP: Exp:plus/recommend.php?action=&aid=1&_FILES[type][tmp_name]=\' or mid=@`\'` /*!50000u ...
- BurpSuite之HTTP brute暴力破解
常规的对username/passwprd进行payload测试,我想大家应该没有什么问题,但对于Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=这样的问题, ...
- ProcDump
https://technet.microsoft.com/en-us/sysinternals/dd996900.aspx
- gdb 多线程调试
gdb 多线程调试 http://hi.baidu.com/hcq11/blog/item/9f5bfc6e696209d680cb4a25.html http://hi.baidu.com/lit ...
- 移动端富文本编辑器artEditor
摘要: 由于手机上打字比较慢,并不适合长篇大论的文章,所以移动端的富文本编辑器很少.artEditor是一款基于jQuery的移动端富文本编辑器,支持插入图片,后续完善其他功能. 插件地址:https ...