LeetCode-Minimum Window Substring-最小窗口子串-滑动窗口算法(尺取法)

https://oj.leetcode.com/problems/minimum-window-substring/

线性复杂度的限制下,考虑使用滑动窗口法。这个方法的思路就是维持一个窗口,窗口向右边界扩张以满足限制条件。窗口左边界收缩以尽量使其最小。

注意这个题目可能是一个典型的滑动窗口方法的实现。外部循环移动左边界i,循环内部扩张右边界p以满足限制条件。并且内外都有终止可能。

使用两个map和一个计数变量以快速统计条件限制的满足情况。

class Solution {
public:
	int n,m;
	string minWindow(string S, string T) {
		map <char,int> cm;
		map <char,int> stat;
		n=S.length();
		m=T.length();
		for (int i=0;i<m;i++){
			if (cm.find(T[i])==cm.end()){
				cm[T[i]]=1;
			}
			else{
				cm[T[i]]++;
			}
			stat[T[i]]=0;
		}
		int res=numeric_limits<int>::max();
		typedef pair <int,int> scpair;
		scpair minw;
		int q=0;
		int count=0;
		for (int i=0;i<n;i++){
			bool endFlag=false;
			while(count<m){
				q++;
				if (q>n){
					endFlag=true;
					break;
				}
				if (cm.find(S[q-1])==cm.end()){continue;}
				if (stat[S[q-1]]<cm[S[q-1]]){
					count++;
				}
				stat[S[q-1]]++;
			}
			if (endFlag){break;}
			if (res > q-i){
				res=q-i;
				minw=scpair(i,q);
			}
			if (cm.find(S[i])==cm.end()){continue;}
			stat[S[i]]--;
			if (stat[S[i]]<cm[S[i]]){count--;}
		}
		if (res>n){return "";}//no win
		return S.substr(minw.first,minw.second-minw.first);
	}
};

  

时间: 2024-08-07 21:19:21

LeetCode-Minimum Window Substring-最小窗口子串-滑动窗口算法(尺取法)的相关文章

[LeetCode] 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 i

[Leetcode] 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

LeetCode: Minimum Window Substring [076]

[题目] 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

[leetcode]Minimum Window Substring @ Python

原题地址:https://oj.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" M

LeetCode &quot;Minimum Window Substring&quot; - STAR

Main reference: http://zhaohongze.com/wordpress/2014/01/04/leetcode-minimum-window-substring/ The ART of counting. So difficult and beautiful problem. It is all about dynamic window maintanence. Essentially, it is still a fancy linear scanning proced

leetCode 76.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 windo

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). Example: Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC" Note: If there is no such window in S

[LeetCode] Minimum Window Subsequence 最小窗口序列

Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W. If there is no such window in S that covers all characters in T, return the empty string "". If there are multiple such minimum-length window

leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)

用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstring(string s) { unordered_map<char,int>mp; int ans=0,now=0;//now is the window's instant length int b=0,e=0;//the window's begin and end int len=s.len