【LeetCode】Minimum Window Substring
来源:互联网 发布:seo招聘信息 编辑:程序博客网 时间:2024/06/16 22:30
Minimum Window Substring
Total Accepted: 6795 Total Submissions: 39203 My Submissions
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.
【解题思路】
最小滑动窗口
按照C/C++描述来说,就是声明两个指针,不断滑动计算要求的最小长度。
具体而言,分为以下几步:
1、声明一个数组inArr,来统计源字符串T中每个字符出现的次数。
2、扫描字符串S,如果该字符存在T中,并且numArr[num1] <= inArr[num1],count++,这句话的意思就是目前所有的字符还不足以包含T中的所有字符。
3、判断count和T.length(),如果相等,就判断首指针是否还可以右移,到不能移动为止,就是一个最短长度。
4、重复2和3,就可以得到最小值。
Total Accepted: 6795 Total Submissions: 39203 My Submissions
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.
【解题思路】
最小滑动窗口
按照C/C++描述来说,就是声明两个指针,不断滑动计算要求的最小长度。
具体而言,分为以下几步:
1、声明一个数组inArr,来统计源字符串T中每个字符出现的次数。
2、扫描字符串S,如果该字符存在T中,并且numArr[num1] <= inArr[num1],count++,这句话的意思就是目前所有的字符还不足以包含T中的所有字符。
3、判断count和T.length(),如果相等,就判断首指针是否还可以右移,到不能移动为止,就是一个最短长度。
4、重复2和3,就可以得到最小值。
Java AC
public class Solution { public String minWindow(String S, String T) { if (S == null || T == null || "".equals(S) || "".equals(T)) {return "";}int size = 123;int inArr[] = new int[size];int numArr[] = new int[size];int len1 = S.length();int len2 = T.length();for (int i = 0; i < len2; i++) {inArr[T.charAt(i)]++;}int count = 0;int start = 0;int minLen = Integer.MAX_VALUE;String minS = "";for (int end = 0; end < len1; end++) {int num1 = S.charAt(end);if (inArr[num1] != 0) {numArr[num1]++;if (numArr[num1] <= inArr[num1]) {count++;}if (count == len2) {while (inArr[S.charAt(start)] == 0|| numArr[S.charAt(start)] > inArr[S.charAt(start)]) {if (numArr[S.charAt(start)] > inArr[S.charAt(start)]) {numArr[S.charAt(start)]--;}start++;}int tempLen = end - start + 1;if (minLen > tempLen) {minLen = tempLen;minS = S.substring(start, end + 1);}}}}return minS; }}
0 0
- LeetCode: Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode : Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- LeetCode: Minimum Window Substring
- [LeetCode]Minimum Window Substring
- [leetcode]Minimum Window Substring
- Leetcode:Minimum Window Substring
- LeetCode-Minimum Window Substring
- [leetcode] Minimum Window Substring
- [leetcode] Minimum Window Substring
- leetcode Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [LeetCode] Minimum Window Substring
- leetcode minimum window substring
- LeetCode - Minimum Window Substring
- LeetCode:Minimum Window Substring
- MFC中的MVC及两者之间的关系
- Kettle 使用备忘录:生成xml文件
- 详解@Autowired、@Qualifier和@Required
- 【Git】git rebase+fix conflict+git add+git rebase --continue或git merge+fix conflict+git add+git commit
- Cocos2dx 3.0 过渡篇(十七) std::bind与CC_CALLBACK不得不说的故事
- 【LeetCode】Minimum Window Substring
- HTML实现图片上传前预览
- Java调试小技巧
- Cocos2dx 3.0 过渡篇(十八) Cocos2d-x 3.0rc,又一种创建项目方式
- 网页中的JavaScript和基本语法
- Cocos2dx 3.0 过渡篇(十九)新鲜出炉的Value该怎么玩
- 黑马程序员—throw和throws的区别
- [LeetCode] Combination Sum
- C#实现QQ空间登录