zoukankan      html  css  js  c++  java
  • [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 S that covers all characters in T, return the emtpy string "".

    If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

    解题:

       string minWindow(string S, string T) 
        {
    		if(S.size() == 0)return "";
    		if(S.size() < T.size())return "";
    		int expect[128] = {0};
    		int appear[128] = {0};
    		int i;
    		int cnt = 0;
    		int start = 0;
    		int min_start = 0;
    		int min_length = INT_MAX;
    		for(i = 0; i < T.size(); i++){
    			expect[T[i]]++;
    		}
    		for(i = 0; i < S.size(); i++){
    			if(expect[S[i]] > 0){
    				appear[S[i]] ++;
    				if(appear[S[i]] <= expect[S[i]])
    					cnt++;
    			}
    			if(cnt == T.size()){
    				while(appear[S[start]] > expect[S[start]]
    				|| expect[S[start]] == 0){
    					appear[S[start]]--;
    					start++;
    				}
    				if(min_length > (i - start + 1)){
    					min_length = i - start + 1;
    					min_start = start;
    				}
    			}
    		}
    		if(min_length == INT_MAX)return "";
    		return S.substr(min_start,min_length);
    	}
    

      30ms,还有优化的空间。

  • 相关阅读:
    http协议
    web应用
    前端基础-jquery
    jQuery的事件
    2.UML类图基本介绍
    1.设计模式的七大原则
    使用OpenFeign远程调用时请求头处理报错问题
    SpringCloud Config-分布式配置中心
    19. 类加载器详解
    18. 类加载过程详解
  • 原文地址:https://www.cnblogs.com/zhutianpeng/p/4292062.html
Copyright © 2011-2022 走看看