76Minimum Window Substring
来源:互联网 发布:jdk源码怎么看 编辑:程序博客网 时间:2024/06/06 05:30
76 Minimum Window Substring
链接:https://leetcode.com/problems/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.
Hide Tags Hash Table Two Pointers String
找S字符串中,含有所有T字符串字符的最小子串。想到的办法从前往后遍历,先找到一个子串,有了两个指针,一个指针在头部,一个指针在尾部。然后头部向前移动,直到不满足含有所有T字符串字符的条件,然后再移动尾部指针,直到满足含有所有T字符串字符的条件。在找到的所有子串中找到最小的子串就可以了。
class Solution {public: bool tablevaild(int *t1,int *t2) { for(int i=0;i<128;i++) { if(t1[i]>t2[i]) return false; } return true; } string minWindow(string s, string t) { int table1[128],table2[128]; memset(table1,0,128*4); memset(table2,0,128*4); int length=t.length(); string result=""; int i,j; for(i=0;i<length;i++) table1[t[i]]++; i=j=s.find_first_of(t); for(;i<=(int)s.length()-length;i++) { if(table1[s[i]]==0) continue; if(tablevaild(table1,table2)) { if(result==""||result.length()>j-i) result=s.substr(i,j-i); } else { for(;j<(int)s.length();j++) { table2[s[j]]++; if(tablevaild(table1,table2)) { j++; if(result==""||result.length()>j-i) result=s.substr(i,j-i); break; } } } table2[s[i]]--; } return result; }};
0 0
- 76Minimum Window Substring
- 76Minimum Window Substring
- 76 - Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- [leetcode 76] Minimum Window Substring
- [leetcode] 76 Minimum Window Substring
- LeetCode(76)Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- leetcode || 76、Minimum Window Substring
- leetcode-76 Minimum Window Substring
- leetcode 76: Minimum Window Substring
- leetcode 76: Minimum Window Substring
- Leetcode#76||Minimum Window Substring
- leetcode 76 Minimum Window Substring
- leetcode 76:Minimum Window Substring
- Leetcode-76 Minimum Window Substring
- LeetCode_OJ【76】Minimum Window Substring
- LA3971 - Assemble
- JavaWeb--Session、Cookie
- 拷贝构造函数和赋值运算符区别
- VS2013+win8编写的C++程序在xp/win7中运行
- 关于项目启动报错一些新手问题
- 76Minimum Window Substring
- 熵
- 数字图像处理中相关和卷积的区别
- 匹配不包含指定中文字符的行
- extJs常用的四种Ajax异步提交
- Algorithms—73.Set Matrix Zeroes
- git 工作笔记(自己常用的 git 命令记录)
- Android属性动画PropertyAnimation系列三之LayoutTransition(布局容器动画)
- Uva10795 - A Different Task