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 ...
随机推荐
- vue小故事之父子(上下级)通信之父传子props
vue小故事之父子(上下级)通信之父传子props vue 父子(上下级)通信 props 或许你对父子通信有点迷糊,为什么这样那样父子之间就可以通信了,以下通过一个小故事来进行解说,故事模型或许有 ...
- 改善EDM数据营销的关键点
EDM数据营销有效增加潜在顾客.提升销售量,而成效得看你的邮件到达率.当点选发送后,邮件需要经过层层关卡才能够进入联络人收件箱,若是你的发送评级越好,邮件越容易进入收件箱.评级非常重要,它能够证明你是 ...
- logistic regression中的cost function选择
一般的线性回归使用的cost function为: 但由于logistic function: 本身非凸函数(convex function), 如果直接使用线性回归的cost function的话, ...
- Apache Kafka系列(六)客制化Serializer和Deserializer
已经迁移,请移步:http://www.itrensheng.com/archives/apache-kafka-repartition
- Centos 搭建DNS服务器
1:安装DNS服务 yum install bind -y 2:修改/etc/named.conf 配置文件 listen-on port 53 { any; }; listen-on-v6 port ...
- 如何修改jar包的某一个class
做了两年的开发,碰见了两次开源包的代码有问题,这次碰见的是wsdl4j.jar 具体问题以后再说,先说说如何修改其中的一个class 使用WinRAR打开(不是解压) 找到你要修改的class文件 右 ...
- 安装SQL Server 2008反复提示需要安装MICROSOFT NET FRAMEWORK 3 5 SP1的一个
在安装过.net framework 4的系统中,安装sql server 2008的安装前提之一MICROSOFT .NET FRAMEWORK 3.5 SP1时,可能已经安装并重启了,还是提示安 ...
- XMLHttpRequest 对象相关
XMLHttpRequest 对象用于在后台与服务器交换数据. 后台 package com.java1234.web; import java.io.IOException; import java ...
- python学习道路即将结束
其实今天算是失眠了,所以打算整理一下自己的学习内容了! 今天是我看视频学习的第六天,已经学习到定义类和对象了,有时候回想python这门语言真的很入门吧,各种语法比较简易能懂. 入门首选 print( ...
- 【LeetCode】309、最佳买卖股票时机含冷冻期
Best Time to Buy and Sell Stock with Cooldown 题目等级:Medium 题目描述: Say you have an array for which the ...