76. Minimum Window Substring (JAVA)
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).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
- If there is no such window in S that covers all characters in T, return the empty string
""
. - If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
重点:
- 滑动窗口
- Map的put,get,判断是否存在,遍历
- String的截取
class Solution {
public String minWindow(String s, String t) {
Map<Character,Integer> target = new HashMap<Character,Integer>();
for(int i = 0; i < t.length(); i++){
if(!target.containsKey(t.charAt(i))) target.put(t.charAt(i),1);
else target.put(t.charAt(i), target.get(t.charAt(i))+1);
} int left = 0;
int right = 0;
int minLength = Integer.MAX_VALUE;
int minLeft = 0;
int minRight = s.length()-1;
Map<Character,Integer> source = new HashMap<Character,Integer>();
source.put(s.charAt(0),1);
while(left<=right){
if(ifContain(source,target)){
if(right-left+1 < minLength){
minLength = right-left+1;
minLeft = left;
minRight = right;
} source.put(s.charAt(left), source.get(s.charAt(left))-1);
left++;
}
else{
right++;
if(right == s.length()) break; if(!source.containsKey(s.charAt(right))) source.put(s.charAt(right),1);
else source.put(s.charAt(right), source.get(s.charAt(right))+1);
}
} if(minLength==Integer.MAX_VALUE) return "";
else return s.substring(minLeft,minRight+1);
} public Boolean ifContain(Map<Character, Integer> source, Map<Character, Integer> target){
for(Character key: target.keySet()){
if(!source.containsKey(key) || source.get(key) < target.get(key)) return false;
}
return true;
}
}
76. Minimum Window Substring (JAVA)的更多相关文章
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 【LeetCode】76. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- 76. Minimum Window Substring
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [leetcode]76. Minimum Window Substring最小字符串窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 76. Minimum Window Substring(hard 双指针)
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LC] 76. Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【一天一道LeetCode】#76. Minimum Window Substring
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- JAVA-ThreadPoolExecutor 线程池
一.创建线程池 /** * @param corePoolSize 核心线程池大小 * 当提交一个任务到线程池时,如果当前 poolSize < corePoolSize 时,线程池会创建一个线 ...
- c++ STL -- set和multiset
set和multiset 1.结构 set和multiset会根据特定的排序原则将元素排序.两者不同之处在于,multisets允许元素重复,而set不允许重复. 只要是assignable.copy ...
- leetcode 328 奇偶链表
更新代码: 开头检测是否需要调整(是否具有第三个节点) 使用三个ListNode* 变量记录奇偶链表的头尾headA,tailA为奇链表,headB为偶数链表,由于只需要最后令tailA->ne ...
- django-登录页面添加验证码
1,安装第三方库 pip install django-simple-captcha 2,注册应用 INSTALLED_APPS = [ 'django.contrib.admin', 'django ...
- web开发(二) Servlet中response、request乱码问题解决
在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6412475.html>,在此仅供学习参考之用. 一.re ...
- 前端UI框架搜集
网址:https://blog.csdn.net/will5451/article/details/80652429?utm_source=blogxgwz6 网址:https://www.cnblo ...
- 十五:jinja2过滤器之实现自定义过滤器
过滤器的本质就是函数,如果在模板中调用这个过滤器,那么就会将这个变量的值作为第一个参数传给过滤器函数,然后将函数的返回值作为滤器的返回值 1.在python文件中写好过滤的函数和逻辑2.将将函数注册到 ...
- 如何在Ubuntu / CentOS 6.x上安装Bugzilla 4.4
这里,我们将展示如何在一台Ubuntu 14.04或CentOS 6.5/7上安装Bugzilla.Bugzilla是一款基于web,用来记录跟踪缺陷数据库的bug跟踪软件,它同时是一款免费及开源软件 ...
- python爬虫常用的库
1,请求:requests requests.get(url, headers) requests.post(url, data=data, files=files) urllib模块: Py ...
- python3.5 append使用
1.从元组中添加 friends=[] tup1=("Jon",35) friends.append(tup1[0]) print(friends[0]) ssh://root@1 ...