[LeetCode]Minimum Window Substring
2015-11-26 20:13
239 查看
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.
双指针,用hash来维护匹配的字符。然后伸缩前指针,注意收缩的原则。如果出现新的数字可以替代现在的前面数字就收缩解集。
class Solution { public: string minWindow(string s, string t) { int min = INT_MAX; int subs = 0; vector<int> appearedNum(300,0); vector<int> expectNum(300,0); for(int i=0; i<t.length(); ++i){ expectNum[t[i]]++; } int start=0,end = 0;//初始化 int count = 0; for(end=0; end<s.length(); ++end){ if(expectNum[s[end]]>0){ appearedNum[s[end]]++; if(appearedNum[s[end]]<=expectNum[s[end]]) count++; }//统计出现数字 if(count == t.length()){//出现可收缩解的情况就收缩头指针 while(appearedNum[s[start]]>expectNum[s[start]]||expectNum[s[start]]==0){ appearedNum[s[start]]--; start++; } if(min>end-start+1){ min = end-start+1; subs = start; } } } if(count<t.length()) return ""; else return s.substr(subs,min); } };
相关文章推荐
- OC中的内存管理
- 使用github+Hexo人人都能拥有一个美美的博客
- 文章标题
- “南大软院大神养成计划“_第十一天的学习“
- C语言练习作业(五)
- python的编码类型转换
- RecycleView Adapter的封装
- C#_Delegate_泛型
- Cocos Studio 2.3.2开发中的Label控件锚点问题等
- JAVA动态绑定的内部实现机制
- python中的urlencode和urldecode
- 延迟加载之ViewStub
- ceph介绍
- 《将博客搬至CSDN》
- 阿里云服务器搭建自己的个人网站之拨开迷雾
- Nginx+Tomcat实现单IP、多域名、多站点的访问
- java多态,如何理解父类引用指向子类对象
- TCP长连接与短连接的区别
- linux下adb连不上android设备解决方法
- 剑指offer前奏二