Java for LeetCode 076 Minimum Window Substring
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.
解题思路:
涉及到查找操作,将T放进tMap中,同时创建一个保存T中所有字符(不重复)的图sMap,用一个计数器表示sMap和tMap的重合程度,一旦sMap包含了tMap即找到了一组解,通过收缩begin指针获取刚好包含tMap的位置,之后就是比较大小了,JAVA实现如下:
public String minWindow(String s, String t) {
HashMap<Character, Integer> tMap = new HashMap<Character, Integer>();
HashMap<Character, Integer> sMap = new HashMap<Character, Integer>();
for (int i=0;i<t.length();i++){
sMap.put(t.charAt(i), 0);
if (!tMap.containsKey(t.charAt(i)))
tMap.put(t.charAt(i), 1);
else
tMap.put(t.charAt(i), tMap.get(t.charAt(i)) + 1);
}
int begin=0,count=0,minBegin=0,length=s.length()+1;;
for(int i=0;i<s.length();i++){
if(!tMap.containsKey(s.charAt(i)))
continue;
sMap.put(s.charAt(i), sMap.get(s.charAt(i))+1);
if(sMap.get(s.charAt(i))<=tMap.get(s.charAt(i)))
count++;
if(count==t.length()){
for(int j=begin;j<=i;j++){
if(!tMap.containsKey(s.charAt(j)))
continue;
if(sMap.get(s.charAt(j))>tMap.get(s.charAt(j))){
sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
continue;
}
sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
count--;
begin=j+1;
if(length>i-j){
length=i-j;
minBegin=j;
}
break;
}
}
}
return length!=s.length()+1?s.substring(minBegin, minBegin+length+1):"";
}
Java for LeetCode 076 Minimum Window Substring的更多相关文章
- [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 ...
- 【leetcode】Minimum Window Substring (hard) ★
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 076 Minimum Window Substring 最小窗口子字符串
给定一个字符串 S 和一个字符串 T,找到 S 中的最小窗口,它将包含复杂度为 O(n) 的 T 中的所有字符.示例:S = "ADOBECODEBANC"T = "AB ...
- Leetcode#76 Minimum Window Substring
原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
随机推荐
- Shell重定向&>file、2>&1、1>&2的区别
shell上: 0表示标准输入 1表示标准输出 2表示标准错误输出 > 默认为标准输出重定向,与 1> 相同 2>&1 意思是把 标准错误输出 重定向到 标准输出. & ...
- BZOJ-1975 魔法猪学院 K短路 (A*+SPFA)
1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1323 Solved: 433 [Submit][Statu ...
- bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]
Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在 ...
- EJB3 QL查询
http://www.blogjava.net/liaojiyong/archive/2008/07/11/56216.html EJB3 QL查询 EJB3的查询语言是一种和SQL非常类似的中间性和 ...
- Centos是什么
Linux是GNU/Linux的缩写,通常指各种Linux发行版的通称. 常见的Linux厂家主要有Redhat/Novell等. Redhat有两大Linux产品系列,其一是免费的Fedora Co ...
- php中单例模式的解析说明
<?php //单例模式 class Dbconn{ private static $_instance=null; protected static $_counter=0; protecte ...
- 使用PPA在Ubuntu上安装php5.4~5.6,7
使用PPA在Ubuntu上安装php5.4~5.6,7 sudo apt-get install software-properties-common sudo add-apt-repository ...
- Initialization of deep networks
Initialization of deep networks 24 Feb 2015Gustav Larsson As we all know, the solution to a non-conv ...
- mysql 服务无法启动 服务没有报告任何错误
问题 解决方法 1.必须保证 mysql 下不存在 data 文件夹,如果存在 data 文件夹,则先删除 mysql 下的 data 文件夹,然后初始化 mysqld --initialize 服务 ...
- 旋转屏幕时,假如自定义的xib大小变了,可能是这个属性没有修改
虽然xib内部启用了自动布局,但是当xib放入外界,xib自身的autoresizing是存在的