LeetCode()Minimum Window Substring 超时,但觉得很清晰。
我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法。
遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中
如果不符合,end++,直到遇到s[end]在t中。
class Solution {
public:
string minWindow(string s, string t) {
        int start=0,end=t.size()-1;
        string res;
        int len=INT_MAX;
        map<char,int> coll;
        for(int i=0;i<t.size();i++)
        {
            coll[t[i]]++;
        }
        while(end<s.size())
        {
            string str=s.substr(start,end-start+1);
            if(check(str,t))
            {
                if(end-start+1 < len)
                {
                    len=end-start+1;
                    res=str;
                }
                while(++start < end && coll[s[start]] == 0);
            }
            else while(++end<s.size() && coll[s[end]] == 0);
        }
        return res;
    }
    bool check(string str,string t)
    {
		map<char,int> coll;
		for(int i=0;i<str.size();i++)
        {
            coll[str[i]]++;
        }
        for(i=0;i<t.size();++i)
		{
			if(coll[t[i]]==0)
				return false;
			else
				coll[t[i]]--;
		}
        return true;
    }
};
看了几个别人的,越发觉得我这个思路很清晰,可惜超时,先这样吧。
LeetCode()Minimum Window Substring 超时,但觉得很清晰。的更多相关文章
- [LeetCode] Minimum Window Substring 最小窗口子串
		Given a string S and a string T, find the minimum window in S which will contain all the characters ... 
- [leetcode]Minimum Window Substring @ Python
		原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ... 
- [LeetCode] Minimum Window Substring  散列映射问题
		题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ... 
- Leetcode Minimum Window Substring
		Given a string S and a string T, find the minimum window in S which will contain all the characters ... 
- [Leetcode] minimum window substring 最小字符窗口
		Given a string S and a string T, find the minimum window in S which will contain all the characters ... 
- Minimum Window Substring @LeetCode
		不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ... 
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
		1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ... 
- 【LeetCode】76. Minimum Window Substring
		Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ... 
- 53. Minimum Window Substring
		Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ... 
随机推荐
- Quality assessment and quality control of NGS data
			http://www.molecularevolution.org/resources/activities/QC_of_NGS_data_activity_new table of contents ... 
- python走起之第三话
			一. SET集合 set是一个无序且不重复的元素集 class set(object): """ set() -> new empty set object set ... 
- HDU-4532 湫秋系列故事——安排座位 组合数学DP
			题意:有来自n个专业的学生,每个专业分别有ai个同学,现在要将这些学生排成一行,使得相邻的两个学生来自不同的专业,问有多少种不同的安排方案. 分析:首先将所有专业的学生视作一样的,最后再乘以各自学生的 ... 
- ElasticSearch学习问题记录——Invalid shift value in prefixCoded bytes (is encoded value really an INT?)
			最近在做一个电商项目,其中商品搜索中出现一个奇怪的现象,根据某个字段排序的时候会出现商品数量减少的情况.按照一般路要么查不出来,要么正常显示,为什么增加了按照销量排序就会出现查询结果减少的情况. 查了 ... 
- 程序设计入门——C语言 第4周编程练习 2  念整数(5分)
			题目内容: 你的程序要读入一个整数,范围是[-100000,100000].然后,用汉语拼音将这个整数的每一位输出出来. 如输入1234,则输出: yi er san si 注意,每个字的拼音之间有一 ... 
- Calendar.get()方法--- WEEK_OF_YEAR 、MONTH、
			1. WEEK_OF_YEAR 一年中的第几周 由于西方的一周指的是:星期日-星期六,星期日是一周的第一天,星期六是一周的最后一天, 所以,使用 calendar.get(Calendar.WEE ... 
- vs---错误收集并自己解决后归纳
			1.C++编译时,出现这样的错误 d:\program files\microsoft visual studio\vc98\include\stdio.h(36) : error C2143: sy ... 
- spring随手笔记2:初始化方法
			1.init-method="init" public class HelloWorldServiceImpl implements HelloWorldService { pri ... 
- Linux下文件和目录的相关操作
			文件和目录的操作命令,按以下思路进行整理,感觉更便于记忆和使用 1.创建一个二进制文件 touch f1 2.向文件中写入数据 echo "hello" >> f1 e ... 
- 【小月博客】用HTML5的File API做上传图片预览功能
			前段时间做了一个项目,涉及到上传本地图片以及预览的功能,正好之前了解过 html5(点击查看更多关于web前端的有关资源) 可以上传本地图片,然后再网上看了一些demo结合自己的需求,终于搞定了.(P ... 
