LC76 Minimum Window Substring
2016-03-07 22:38
309 查看
利用哈希表和滑动窗口来做题。一开始窗口内没有包含所有T的字符,扩大窗口直到包含T所有字符为止。然后再将窗口的左端向右移动,直到不能移动为止(再移动的话窗口内就没有所有T的字符了)。然后再移动窗口右端。如此循环。
View Code
class Solution { public: string minWindow(string s, string t) { string str=""; if(s==""||t==""||(s.size()<t.size())) return str; vector<int> expect(256,0); vector<int> real(256,0); for(int i=0;i<t.size();i++) expect[t[i]]++; int front=0; int end=0; int len=999999999; int start=0; for(;end<s.size();end++) { if(expect[s[end]]>0) { real[s[end]]++; if(real[s[end]]<=expect[s[end]]) count++; } if(count==t.size()) { while(expect[s[front]]==0||real[s[front]]>expect[s[front]]) { real[s[front]]--; front++; } if(len>end-front+1) { len=end-front+1; start=front; } } } return (999999999==len)?"":s.substr(start,len); } };
View Code
相关文章推荐
- HTTP请求与响应
- Linux系统平均负载3个数字的含义
- Servlet 工作原理解析
- 毕业设计第二十天
- 杭电1200
- 5分钟搞懂后缀数组
- 在Android开发中遇到java.lang.NoClassDefFoundError的一般解决办法
- jsp(Java Server Pages)和js(JavaScript)的区别:
- codeforces 651B B. Beautiful Paintings
- CGRect转为string
- 4. PathVariable、RequestParam、 CookieValue、SessionAttributes、ResponseBody、RequestHeader注解类
- 1008. Elevator (20)
- Oracle 11g RAC客户端使用SCAN IP无法连接问题
- hiho一下 第八十八周
- 这两天做leetcode的心得
- 【2016年】-身往何方
- SpringMVC-forward和redirect
- LeetCode 2016.3.7 282,241
- Oracle 11g RAC客户端使用SCAN IP无法连接问题
- 无线板卡接口定义