【Leetcode】Minimum Window Substring
2016-05-24 21:04
344 查看
题目链接:https://leetcode.com/problems/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 =
T =
Minimum window is
Note:
If there is no such window in S that covers all characters in T, return the empty string
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
思路:
时间太久 我也忘了咋做的了= =有时间再补
算法:
public String minWindow(String s, String t) {
String result = "";
StringBuffer res = new StringBuffer();
HashMap<String, Integer> tm = new HashMap<String, Integer>();// 维护窗口内需要的t字符的个数,如果某字符值小于0表示该窗口有多于需要的字符个数
int minLen = Integer.MAX_VALUE, start = 0, end = 0, size = t.length();// size
// 表示剩余要匹配t的字符个数
for (int i = 0; i < t.length(); i++) { // 初始化
String key = t.charAt(i) + "";
if (tm.containsKey(key)) {
tm.put(key, tm.get(key) + 1);
} else {
tm.put(key, 1);
}
}
while (end < s.length()) {
while (end < s.length() && size > 0) {
String key = s.charAt(end) + "";
if (tm.containsKey(key)) {
if (tm.get(key) > 0) { // 如果需要该字符
size--;
}
tm.put(key, tm.get(key) - 1);
}
res.append(key);
end++;
}// 已经找到一个window包含了t所有字符
while (size == 0 && start < end) {
if (minLen > end - start) { // 更新窗口大小
minLen = end - start;
result = s.substring(start, end);
}
String key = s.charAt(start) + "";
if (tm.containsKey(key)) {
if (tm.get(key) == 0) { // 如果窗口删除该字符,则跟t的匹配缺少该字符
size++;
}
tm.put(key, tm.get(key) + 1);
res.deleteCharAt(0);
}
start++;
}
}
return result;
}
题目:
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 empty string
"".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
思路:
时间太久 我也忘了咋做的了= =有时间再补
算法:
public String minWindow(String s, String t) {
String result = "";
StringBuffer res = new StringBuffer();
HashMap<String, Integer> tm = new HashMap<String, Integer>();// 维护窗口内需要的t字符的个数,如果某字符值小于0表示该窗口有多于需要的字符个数
int minLen = Integer.MAX_VALUE, start = 0, end = 0, size = t.length();// size
// 表示剩余要匹配t的字符个数
for (int i = 0; i < t.length(); i++) { // 初始化
String key = t.charAt(i) + "";
if (tm.containsKey(key)) {
tm.put(key, tm.get(key) + 1);
} else {
tm.put(key, 1);
}
}
while (end < s.length()) {
while (end < s.length() && size > 0) {
String key = s.charAt(end) + "";
if (tm.containsKey(key)) {
if (tm.get(key) > 0) { // 如果需要该字符
size--;
}
tm.put(key, tm.get(key) - 1);
}
res.append(key);
end++;
}// 已经找到一个window包含了t所有字符
while (size == 0 && start < end) {
if (minLen > end - start) { // 更新窗口大小
minLen = end - start;
result = s.substring(start, end);
}
String key = s.charAt(start) + "";
if (tm.containsKey(key)) {
if (tm.get(key) == 0) { // 如果窗口删除该字符,则跟t的匹配缺少该字符
size++;
}
tm.put(key, tm.get(key) + 1);
res.deleteCharAt(0);
}
start++;
}
}
return result;
}
相关文章推荐
- HDU1012 u Calculate e
- netty 学习 (3)发送对象
- Broadcast和ContentProvider
- leetcode笔记—关于动态规划
- Spring学习(五)Spring开发包介绍
- RxJava 操作符flatmap
- HDFS Federation (读书笔记)
- C++第六次实验——作业
- C++类对象指针
- Java 数组 foreach
- GEEK编程练习— —三数求和
- UESTC 1253 阿里巴巴和n个大盗(博弈)
- c++实验6数组合并
- Linux下redis的安装
- Codeforces Round #326 (Div. 2)-Duff and Meat
- BZOJ 1295 最长距离
- iOS开发之高级视图—— UITableView(一)简单例子
- 第十三周--分数类中的运算符重载
- svn 清理失败 (cleanup 失败) 的解决方法
- 最小生成树 套餐