• 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);
    	}
    };
    
  • 相关阅读:
    精通javascript、javascript语言精粹读书笔记
    javascript语言精粹。3、对象;4、函数;深入原型链,对象的原理!
    权限管理
    javascript碎碎念(面向对象备忘)
    几个JavaScript面试题
    JavaScript类属性的定义方法和区别
    j
    Java调用SQL存储过程 输入输出参数(转)
    Java中文乱码解决
    js中eval详解
  • 原文地址:https://www.cnblogs.com/lengender-12/p/6927167.html
Copyright © 2020-2023  润新知