Minimum Window Substring 最小窗口覆盖所有字串
来源:互联网 发布:解剖学软件哪个好 编辑:程序博客网 时间:2024/06/05 00:40
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 empty string ""
.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
class Solution {public://双指针滑动 求最小窗口覆盖所有字串//INT_MAX 2147483647 #include<limits.h> string minWindow(string s, string t) { int sLen=s.size(); int tLen=t.size(); int tHash[256],dp[256]; memset(tHash,0,sizeof(tHash));//t字符的个数 memset(dp,0,sizeof(dp));//计算字符数 for(int i=0;i<tLen;i++){ tHash[t[i]]++; } int count=0; int min=INT_MAX,start=0; int left=0,right=0; for(right=0;right<sLen;right++){ if(tHash[s[right]]==0)//字串没有该字符 continue; dp[s[right]]++; if(dp[s[right]]<=tHash[s[right]])//该字符增加一个 count++; if(count==tLen){ while(left<right){ if(tHash[s[left]]==0){//字串没有该字符 left++; continue; } if(dp[s[left]]>tHash[s[left]]){ dp[s[left]]--; left++; }else{ break; } } if(right-left+1<min){ min=right-left+1; start=left; } } } if(min==INT_MAX)//未找到 return ""; //string res=s.substr(start,min); //三种赋值res的方法都可以 //string res(s,start,min); string res; for(int i=start;i<start+min;i++) res+=s[i]; return res; }};
0 0
- Minimum Window Substring 最小窗口覆盖所有字串
- 字符串窗口覆盖 Minimum Window Substring
- Minimum Window Substring 最小窗口子串
- Minimum Window Substring:最小窗口子串
- Minimum Window Substring 最小覆盖子串算法
- Minimum Window Substring 最小子串窗口@LeetCode
- Minimum Window Substring 最小子串窗口@LeetCode
- (每日算法)LeetCode---Minimum Window Substring (最小子串窗口)
- LeetCode 76. Minimum Window Substring(最小窗口子串)
- leetcode:Minimum Window Substring(最小覆盖子串)【面试算法题】
- Minimum Window Substring(最小覆盖子串)【面试算法题】
- Hust oj 1758 Minimum Window Substring(最小覆盖子串问题)
- 32.Minimum Window Substring-最小子串覆盖(中等题)
- Minimum Window Substring 包含串T的在串S中的最小窗口
- LeetCode OJ 之 Minimum Window Substring (最小窗口子串)
- leetCode 76.Minimum Window Substring(最小窗口子串) 解题思路和方法
- Leetcode 76. Minimum Window Substring 最小子串窗口 解题报告
- Leetcode 76 Minimum Window Substring(最小窗口的子串)
- Android代码优化小技巧总结
- HTML5试题
- 对多线程通信的优化
- 选择控件 UISegmentedController
- 测试UI的快捷入口
- Minimum Window Substring 最小窗口覆盖所有字串
- IOS 项目的框架搭建
- cpuacct (CPU Accounting Controller)
- CSS的半透明处理
- 转 -- mariadb galera集群配置
- Android string %1$s %1$d
- 从无到有(统计、请求、MVC、模块化)H5开发须知--一个完整的实例
- eclipse debug技巧
- spring MVC 配置返回JSON格式的数据模型