zoukankan      html  css  js  c++  java
  • Leetcode: 76. Minimum Window Substring

    Description

    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.

    思路

    • 刚开始自己用unordered_map写了一个出来,159ms...
    • 往上找了一个,用两个hash数组做的,确实比我叼。

    代码

    • unordered_map
    class Solution {
    public:
     string minWindow(string s, string t) {
    		int slen = s.size(), tlen = t.size();
    		if (slen == 0 || tlen == 0) return "";
    
    		string res = "";
    		int start = 0, end = -1;
    		int i = 0;
    		unordered_map<char, int> tMap;
    		unordered_map<char, int> cMap;
    		unordered_map<char, int> helpMap;
    
    		for (i = 0; i < tlen; ++i)
    			tMap[t[i]]++;
    
    		i = 0;
    		while (i < slen){
    			if (tMap.find(s[i]) == tMap.end())
    				i++;
    			else break;
    		}
    
    		start = i;
    
    		while (i < slen){
    			if (tMap.count(s[i]) > 0){
    				helpMap[s[i]]++;
    				if (tMap[s[i]] > cMap[s[i]])
    					cMap[s[i]]++;
    			}
    
    			if (tMap == cMap){
    				do{
    					if (res == "" || res.size() > (i - start + 1)){
    						res = s.substr(start, i - start + 1);
    					}
    
    					if (cMap[s[start]] > 1)
    						cMap[s[start]]--;
    					else cMap.erase(s[start]);
    
    					if (helpMap[s[start]] > 1)
    						helpMap[s[start]]--;
    					else helpMap.erase(s[start]);
    
    					if (helpMap[s[start]] > cMap[s[start]])
    						cMap[s[start]]++;
    
    					int j = start + 1;
    					while (j <= i){
    						if (tMap.find(s[j]) == tMap.end())
    							j++;
    						else break;
    					}
    
    					start = j;
    				} while (cMap == tMap);
    			}
    			
    			i++;
    		}
    
    		if (tMap == cMap && (res == "" || res.size() > (slen - start)))
    				res = s.substr(start, i - start + 1);
    		return res;
    	}
    };
    
    • hash数组
    class Solution {
    public:
       string minWindow(string s, string t) 
    	{
    		string res;
    		if (s.size() < t.size()) return res;
    	    vector<int> flag(256, 0);
    	    vector<int> hash(256, 0);
    	    for(int i = 0; i < t.size(); ++i){
    	        flag[t[i]]++;
    	        hash[t[i]]++;
    	    }
    	   
    	    int count = 0;
    	    int start = 0, minsize = s.size() + 1, minflag = -1;
    	    for(int i = 0; i < s.size(); ++i){
    	        if(flag[s[i]] > 0){
    	            if(--hash[s[i]] >= 0)  //这个地方
    	               count++;
    	               
    	           while(count == t.size()){
    	                if(i - start + 1 < minsize){
    	                   minsize = i - start + 1;
    	                   minflag = start;
    	               }
    	               
    	               //这个地方,思路确实巧妙,不得不服
    	               if(flag[s[start]] > 0){
    	                   if(++hash[s[start]] > 0)
    	                        --count;
    	               }
    	               ++start;
    	           }
    	        }
    	        
    	    }
    	    
    	    if(minsize > s.size()) return "";
    	    return s.substr(minflag, minsize);
    	}
    };
    
  • 相关阅读:
    FTP-实例(Md5验证)
    Socket-实例
    函数对象、函数嵌套、名称空间与作用域、装饰器
    Docker——手动创建镜像
    Docker——桥接网络配置
    Docker——网络和存储(数据卷)
    Docker-PS命令解析
    面试题44:扑克牌的顺子
    面试题42:翻转单词顺序VS左旋转字符串
    面试题41:和为s的两个数字VS和为s的连续正数序列
  • 原文地址:https://www.cnblogs.com/lengender-12/p/6927167.html
Copyright © 2011-2022 走看看