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);
    	}
    };
    
  • 相关阅读:
    基于项目中遇到的技术问题,谈谈SharedPreferences的使用的注意问题
    mongodb数据库从库同步主库维护js脚本
    MongoDB数据库日志备份压缩脚本
    mongodb数据库磁盘碎片整理。
    mongodb表字段处理生成域名字段
    根据当前进程号,获取进程下线程数目
    mongodb mapreduce示例
    MongoDB数据库库级锁研究分析
    mongodb库表信息监控脚本
    利用JAVA设计一个可视化日历
  • 原文地址:https://www.cnblogs.com/lengender-12/p/6927167.html
Copyright © 2011-2022 走看看