博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Minimum Window Substring
阅读量:4150 次
发布时间:2019-05-25

本文共 2586 字,大约阅读时间需要 8 分钟。

class Solution {//need2FT + hasFT + two pointer + count(check if satisfied)//O(n)public:	string minWindow(string S, string T) {		// Start typing your C/C++ solution below		// DO NOT write int main() function		int lenT = T.size();		int lenS = S.size();		if(lenT <= 0 || lenT > lenS) return string("");//illegal input		int need2FT[256] = {0};		int hasFT[256] = {0};		for (int i = 0; i < lenT; ++i)//init need2FT 			need2FT[T[i]]++;		int start, end;//two pointer to sliding window		start = end = 0;		int count = 0;		int minStart, minEnd, minLen;		minLen = INT_MAX;		for ( ; end < lenS; ++end)		{			if(0 == need2FT[S[end]]) continue;//unrelated character			hasFT[S[end]]++;			if (hasFT[S[end]] <= need2FT[S[end]])				count++;			if (count == lenT)//maintain the minimum sliding window			{				while (hasFT[S[start]] > need2FT[S[start]] || 0 == need2FT[S[start]])				{					if(0 != need2FT[S[start]])						hasFT[S[start]]--;					start++;					if(start >= lenS)						break;				}				int newWinLen = end-start+1;				if (newWinLen < minLen)				{					minStart = start; 					minEnd = end;					minLen = newWinLen;				}			}			if(start >= lenS) break;					}		if(minLen == INT_MAX) return string("");		//then return the qualified sliding window		return S.substr(minStart, minLen);	}};

second time

class Solution {public:    string minWindow(string S, string T) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector
cntTable(256, 0); vector
checkTable(256, 0); for(int i = 0; i < T.size(); ++i) checkTable[T[i]]++; //find the first window int validCnt = 0; int end; for(end = 0; end < S.size(); ++end) { if(cntTable[S[end]] < checkTable[S[end]]) validCnt++; cntTable[S[end]]++; if(validCnt == T.size()) break; } if(end == S.size()) return ""; int minStart = 0; while(minStart < S.size() && cntTable[S[minStart]] > checkTable[S[minStart]]) cntTable[S[minStart++]]--; int minEnd = end; //try to find a minimum one int curStart = minStart; for(int i = minEnd+1; i < S.size(); ++i) { cntTable[S[i]]++; int newStart = curStart; while(cntTable[S[newStart]] > checkTable[S[newStart]]) { cntTable[S[newStart]]--; newStart++; } curStart = newStart; if(i-newStart < minEnd-minStart) minStart = newStart, minEnd = i; } return S.substr(minStart, minEnd-minStart+1); }};

转载地址:http://wmxti.baihongyu.com/

你可能感兴趣的文章
引用还是指针?
查看>>
checkio-non unique elements
查看>>
checkio-medium
查看>>
checkio-house password
查看>>
checkio-moore neighbourhood
查看>>
checkio-the most wanted letter
查看>>
Redis可视化工具
查看>>
大牛手把手带你!2021新一波程序员跳槽季,全套教学资料
查看>>
Guava Collections API学习之AbstractMapBasedMultimap
查看>>
Guava Collections API学习之HashBiMap
查看>>
Guava Collections API学习之Bimap
查看>>
Guava Collections API学习之Multisets
查看>>
Guava API学习之Resources
查看>>
Guava API学习之CharSequenceReader
查看>>
Guava API学习之Range
查看>>
Guava API学习之RangeSet
查看>>
Guaval API学习之RangeMap
查看>>
JS定时器执行某个动作,可页面动态展示时间走动
查看>>
Tips展开关闭问答代码
查看>>
jQuery仿新浪网“返回顶部”效果
查看>>