[LeetCode] Minimum Window Substring
来源:互联网 发布:面包车拉货选什么软件 编辑:程序博客网 时间:2024/06/04 00:20
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.
滑动子窗口(为了让代码精简,用int[128],不要用HashMap):
public class Solution { public String minWindow(String s, String t) {int[] map=new int[128];int[] map2=new int[128];for(int i=0;i<t.length();i++) map[t.charAt(i)]++;int n=0;int reStart=0,reEnd=Integer.MAX_VALUE;int start=0;for(int i=0;i<s.length();i++){char ch=s.charAt(i);if(map[ch]>0){ if(map2[ch]<map[ch]) n++;map2[ch]++;}if(n>=t.length()){int k=start;for(;k<=i;k++){char c=s.charAt(k);if(map[c]==0) continue;if(map2[c]>map[c]){map2[c]--;}else break;}start=k;if(reEnd-reStart>i-start){reEnd=i;reStart=start;}}}if(reEnd==Integer.MAX_VALUE) return "";else return s.substring(reStart,reEnd+1);}}
阅读全文
1 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
- 数学问题——找“自守数”
- 日语语法(五):副词
- grep 命令详解
- 算法第4版(谢路云译)学习笔记(3) -- 字符串连接操作的时间复杂度
- 音频,视频时间戳计算及每帧播放时间
- [LeetCode] Minimum Window Substring
- 区块链开发专题(数字货币转账手续费是多少是如何计算的)
- 课后练习8.15:最大公共子图问题
- Eclipse离线安装TestNG
- Lua表达式
- 数据库数值函数的解析及使用
- 如何映射本地虚拟机或远程服务器磁盘到本地
- java的日期类
- 《Spring实践——不一样的'HelloWorld'》