[Leetcode] Minimum Window Substring (Java)
来源:互联网 发布:21天学通c语言 编辑:程序博客网 时间:2024/06/03 09:26
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.
在S中找到包含T所有字符的最小窗口(类似于文本摘要提取)
用两个指针:start、end
1)若未包含所有字符,则end++;
2)若包含所有字符,则start++,直到不包含为止,得到窗口大小并与当前最小窗口比较并更新。
public class Solution { public String minWindow(String S, String T) {int min = S.length()+1;int minStart = 0;boolean flag = false;int count=T.length();int[] count1 = new int[256];Map<Character, Boolean> map = new HashMap<Character, Boolean>();int start=0,end=0;for(int i=0;i<T.length();i++){count1[T.charAt(i)]++;map.put(T.charAt(i), true);}for(end=0;end<S.length();end++){if(map.containsKey(S.charAt(end))&&map.get(S.charAt(end))){count1[S.charAt(end)]--;if(count1[S.charAt(end)]>=0){count--;}}if(count==0){while(count==0){if(map.containsKey(S.charAt(start))&&map.get(S.charAt(start))){count1[S.charAt(start)]++;if(count1[S.charAt(start)]>0)count++;}start++;}if(end-start+2<min){min=end-start+2;minStart=start-1;}}}if(min==S.length()+1)return ""; return S.substring(minStart,minStart+min); }}
0 0
- [Leetcode] Minimum Window Substring (Java)
- Minimum Window Substring LeetCode java
- [LeetCode][Java] 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
- XML 三种解析方式简介
- 使用dd来备份还原mbr,分区表
- vlc-android 中调用用libvlcjni.so实现流媒体播放,自己使用libvlcjni.so
- UVa:140 Bandwidth
- C#操作excel
- [Leetcode] Minimum Window Substring (Java)
- 推荐一款专为MySQL设计的ER/数据库建模工具MySQL Workbench
- 网站回到顶部js特效
- c编程规范细节
- 最新的MyEclipse 2014代码自动提示设置——包括自动上屏处理
- 静态库和动态库
- TreeMap源码分析三
- 1.android离线包下载,安装(工具类)
- App产品难以在市场中取得预期效果的原因