**LeetCode-Minimum Window Substring
来源:互联网 发布:并查集算法c代码 编辑:程序博客网 时间:2024/06/06 05:42
这种题都是hashmap加two pointers思想 但是移动start pointer的rules不一样 以及判断是否包含了所有的字母找到了答案的方法也不太一样
这个题也是先统计总共需要哪些字母 需要多少个 然后开始扫
同样是要找到有效count等于需要的总count之后表示找到了答案 然后这个题的start指针记得要挪动到下一个valid字母处
public class Solution { public String minWindow(String s, String t) { HashMap<Character, Integer> hist = new HashMap<Character, Integer>(); for ( int i = 0; i < t.length(); i ++ ){ if ( hist.containsKey ( t.charAt( i ))) hist.put ( t.charAt( i ), hist.get (t.charAt(i)) + 1); else hist.put ( t.charAt( i ), 1 ); } HashMap<Character, Integer> map = new HashMap<Character, Integer>(); int start = 0; String minSub = ""; int minLen = Integer.MAX_VALUE; int count = 0; for ( int i = 0; i < s.length(); i ++ ){ char c = s.charAt ( i ); if ( hist.containsKey ( c ) ){ if ( map.containsKey( c ) ) map.put ( c, map.get ( c ) + 1 ); else map.put ( c, 1 ); if ( map.get ( c ) <= hist.get ( c )) count ++; } if ( count == t.length()){ while( !hist.containsKey(s.charAt(start)) || map.get(s.charAt(start)) > hist.get(s.charAt(start))){ if( map.containsKey(s.charAt(start))) map.put(s.charAt(start), map.get(s.charAt(start))-1); start++; } if ( i - start + 1 < minLen ){ minLen = i - start + 1; minSub = s.substring( start, i + 1 ); } count --; map.put(s.charAt(start), map.get(s.charAt(start))-1); start ++; } } return minSub; }}
0 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
- Python基本语法_异常处理详解
- 2077(汉诺塔4
- Mac OS 配置Maven
- JavaScript中寄生组合式继承的理解
- 第5章(4) 可变参数列表
- **LeetCode-Minimum Window Substring
- 机器学习笔记_ 降维_3:SVD
- Win7中安装不了Arduino驱动怎么办
- 求最大公约数与最小公倍数——辗转相除法
- thrift安装以及编写小程序
- 12个球找出奇异球
- CentOS 7安装后实用优化(不定期更新)
- python 入门
- 第5章(5) 枚举类型