76.窗口滑动最小子串
来源:互联网 发布:手机淘宝如何五星好评 编辑:程序博客网 时间:2024/05/29 15:02
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".
参考答案(c++):
class Solution {public: string minWindow(string s, string t) { vector<int> map(128,0); for(auto c: t) map[c]++; int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0; while(end<s.size()){ if(map[s[end++]]-->0) counter--; //in t while(counter==0){ //valid if(end-begin<d) d=end-(head=begin); if(map[s[begin++]]++==0) counter++; //make it invalid } } return d==INT_MAX? "":s.substr(head, d); }};
性能:
参考答案(python):
class Solution(object): def minWindow(self, s, t): need, missing = collections.Counter(t), len(t) i = I = J = 0 for j, c in enumerate(s, 1): missing -= need[c] > 0 need[c] -= 1 if not missing: while i < j and need[s[i]] < 0: need[s[i]] += 1 i += 1 if not J or j - i <= J - I: I, J = i, j return s[I:J]
性能:
阅读全文
0 0
- 76.窗口滑动最小子串
- 最小窗口子串
- 滑动窗口1:最大窗口子串
- LeetCode 76. Minimum Window Substring(最小窗口子串)
- Minimum Window Substring 最小窗口子串
- Minimum Window Substring:最小窗口子串
- leetCode 76.Minimum Window Substring(最小窗口子串) 解题思路和方法
- Leetcode 76. Minimum Window Substring 最小子串窗口 解题报告
- Minimum Window Substring 最小子串窗口@LeetCode
- Minimum Window Substring 最小子串窗口@LeetCode
- (每日算法)LeetCode---Minimum Window Substring (最小子串窗口)
- 求满足某种条件的子串问题- 滑动窗口法
- 最小覆盖子串
- 最小子串
- 最小子串覆盖
- 最小覆盖子串
- 最小子串查找
- 最小子串
- 买苹果---动态规划
- Node.js是如何解决服务器高性能瓶颈问题的
- 2017ACM/ICPC广西邀请赛-重现赛
- 灰度共生矩阵 matlab里的 graycomatrix函数
- 关于Hashtable和HashMap的区别 -- Java 基础
- 76.窗口滑动最小子串
- Android启动模式的探索
- Spring学习之Spring概述(一)
- Phpstudy+WordPress安装详解
- nodejs的路由机制
- HTML文档--超链接等基本编辑命令
- 关于QR二维码的编码模式
- 小熊吃糖(结构体排序)
- 2014 Multi-University Training Contest 1 A(hdu 4861 打表找规律)