LeetCode Minimum Window Substring
2016-02-21 13:40
344 查看
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.
题意:给出字符串s和t,求t在s中出现的最小窗口
思路:统计字符串t 的计数,在遍历字符串s时,计数减1,如果计数等于0,同时更新最小窗口的起始位置和长度。
代码如下:
解法二:先统计字符串s的计数,在遍历t过程中,计数减1。最后判断剩下的是否包含有t
class Solution
{
public String minWindow(String s, String t)
{
int[] c = new int[256];
for (char ch : s.toCharArray()) ++c[ch];
for (char ch : t.toCharArray())
{
if (--c[ch] < 0) return "";
}
int start = 0, end = s.length() - 1;
while (--c[s.charAt(end)] >= 0) --end;
++c[s.charAt(end)];
while (--c[s.charAt(start)] >= 0) ++start;
++c[s.charAt(start)];
String result = s.substring(start, end + 1);
int min = end - start + 1;
while (++end < s.length())
{
++c[s.charAt(end)];
while (--c[s.charAt(start)] >= 0) ++start;
++c[s.charAt(start)];
if (end - start + 1 < min)
{
result = s.substring(start, end + 1);
min = end - start + 1;
}
}
return result;
}
}
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.
题意:给出字符串s和t,求t在s中出现的最小窗口
思路:统计字符串t 的计数,在遍历字符串s时,计数减1,如果计数等于0,同时更新最小窗口的起始位置和长度。
代码如下:
class Solution { public String minWindow(String s, String t) { int[] c = new int[128];; for (int i = 0; i < t.length(); i++) { c[t.charAt(i)]++; } int count = t.length(); int start = 0, end = 0, min_len = Integer.MAX_VALUE; int min_start = 0; while (end < s.length()) { if (c[s.charAt(end)] > 0) count--; c[s.charAt(end)]--; while (0 == count) { if (end - start + 1 < min_len) { min_start = start; min_len = end - start + 1; } c[s.charAt(start)]++; if (c[s.charAt(start)] > 0) count++; start++; } end++; } if (min_len != Integer.MAX_VALUE) return s.substring(min_start, min_start + min_len); return ""; } }
解法二:先统计字符串s的计数,在遍历t过程中,计数减1。最后判断剩下的是否包含有t
class Solution
{
public String minWindow(String s, String t)
{
int[] c = new int[256];
for (char ch : s.toCharArray()) ++c[ch];
for (char ch : t.toCharArray())
{
if (--c[ch] < 0) return "";
}
int start = 0, end = s.length() - 1;
while (--c[s.charAt(end)] >= 0) --end;
++c[s.charAt(end)];
while (--c[s.charAt(start)] >= 0) ++start;
++c[s.charAt(start)];
String result = s.substring(start, end + 1);
int min = end - start + 1;
while (++end < s.length())
{
++c[s.charAt(end)];
while (--c[s.charAt(start)] >= 0) ++start;
++c[s.charAt(start)];
if (end - start + 1 < min)
{
result = s.substring(start, end + 1);
min = end - start + 1;
}
}
return result;
}
}
相关文章推荐
- Android Studio各个版本的区别
- 基于友善之臂ARM-ContexA9-ADC驱动开发
- 基于友善之臂ARM-ContexA9-ADC驱动开发
- 基于友善之臂ARM-ContexA9-ADC驱动开发
- Linux 网络环境查看命令
- C/C++程序开发中实现信息隐藏的三种类型
- srand((unsigned int)time(NULL))引发的思考
- main 函数
- 组织目标与个人目标
- java中如何输入char类型
- Solr配置扩展词/自定义词库(IK版)、如何做逻辑与逻辑或即AND OR查询
- vim+python+OpenCV学习二 : 灰度图像 保存图像
- Educational Codeforces Round 8 A. Tennis Tournament 暴力
- WebService梳理
- Java 序列化Serializable详解
- 单片机系列知识1--单片机的内外结构
- JAVA多线程-Lock的使用(二)-公平锁与非公平锁
- [Node入门] => 读书笔记(三)
- HDOJ 5630-Rikka with Chess
- 【慕课笔记】第二章 认识JAVA中的字符串 第5节 认识JAVA中stringBuilder类