LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
题目: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.
上一个系列我们讲到了O(N*M)的解法,这里主要的矛盾是,当遇到一个合法的window的时候,我们无法找到高效的办法(最好是O(1))来找到这个window的起点。下面的O(NlogM)的解法在上一个解法的基础上,使用一个SoredMap来存放当前的window。
该map的key是S的index,value对应S中该index的字母。因为是SortedMap,在发现合法的Window后,总是能通过firstKey和lastKey得到window的长度。而且也免除了使用额外的bit_status来检验合法window的需要。
同样的,当一个字母收集超过了T中要求的数目,那么删除charAppearenceRecorder中对应链表的头节点,同时,还需删除SortedMap中该index为key的entry。代码如下:
1 publicString minWindow2(String S, String T){2 HashMap<Character, Integer> needToFill = new HashMap<Character, Integer>();3 HashMap<Character, LinkedList<Integer>> charAppearenceRecorder = new HashMap<Character, LinkedList<Integer>>();4 SortedMap<Integer, Character> winMap = new TreeMap<Integer, Character>();5 int minWinStart = -1;6 int minWinEnd =S.length();7 for(int i = 0; i < T.length(); i++){8 if(!needToFill.containsKey(T.charAt(i))){9 needToFill.put(T.charAt(i), 1);10 charAppearenceRecorder.put(T.charAt(i), new LinkedList<Integer>());11 }else{12 needToFill.put(T.charAt(i), needToFill.get(T.charAt(i)) + 1);13 }14 }15 16 for(int i = 0; i < S.length(); i++){17 char c =S.charAt(i);18 if(needToFill.containsKey(c)){19 LinkedList<Integer> charList =charAppearenceRecorder.get(c);20 if(charList.size() <needToFill.get(c)){21 charList.add(i);22 winMap.put(i, c);23 }else{24 //如果某个字母收集过了,需要删除该字母出现的最小的index,保留靠右的部分 25 int idxToErase =charList.removeFirst();26 winMap.remove(idxToErase);27 winMap.put(i, c);28 charList.add(i);29 }30 if(winMap.size() ==T.length()){31 int start =winMap.firstKey();32 int end =winMap.lastKey();33 if(end - start < minWinEnd -minWinStart){34 minWinStart =start;35 minWinEnd =end;36 }37 }38 }39 }40 41 return minWinStart != -1 ? S.substring(minWinStart, minWinEnd + 1) : "";42 }
O(NlogM)
代码的流程很符合O(N*M)的方法。就不“举一个栗子”了吧。
总结下:
1.合理运用embeded的数据结构。
转载于:https://www.cnblogs.com/lichen782/p/leetcode_Minimum_Window_Substring_2.html
LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...相关推荐
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- 【打印代码+好好理解+子串问题】LeetCode 76. Minimum Window Substring
LeetCode 76. Minimum Window Substring 字符串子串问题!!!理解这题真是花了很大的功夫!!! 参考链接:https://blog.csdn.net/weixin_4 ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- LeetCode 76. Minimum Window Substring / 567. Permutation in String
76. Minimum Window Substring 典型Sliding Window的问题,维护一个区间,当区间满足要求则进行比较选择较小的字串,重新修改start位置. 思路虽然不难,但是如何 ...
- [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 charact ...
- 72.Minimum Window Substring(最小子串窗口)
Level: Hard 题目描述: Given a string S and a string T, find the minimum window in S which will contain ...
- Minimum Window Substring 最小覆盖子串算法
转载:https://blog.csdn.net/fly_yr/article/details/51134340 题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串s ...
最新文章
- Python模拟哲学家进餐问题
- php在window磁盘管理,Windows Server 2008R2设置磁盘阵列
- java内存溢出让tomcat停止_java - 使用JVM Open J9一段时间后,应用程序(tomcat)停止响应 - 堆栈内存溢出...
- ajax post django,Django中的Ajax POST请求失败
- 蚂蚁金服ATEC城市峰会上海举行,三大发布迎接金融科技2019
- 四、OSPF配置实验
- Java简单猜数字游戏
- 关于“DEP数据执行保护”的解决方案
- 【C语言题解】将数字金额翻译成中文大写金额
- 金融风险管理 思维导图
- C#设计模式之四建造者模式(Builder Pattern)【创建型】
- S7-1200 PLC 激活系统时钟存储位后,相应的位没有工作?
- JavaScript网页滚动距离
- 删除win10系统默认微软输入法
- LayUI使用Echarts实现统计图
- 「津津乐道播客」#231. 串台:跟『新世相』聊聊被刷屏的『沈老师』
- FreeCAD二次开发:集成二维CAD控件MxDraw
- MySQL常用监控指标及监控方法
- php遍历关联数组详解,php遍历关联数组
- git 分支合并冲突解决
热门文章
- strsep()函数:字符串切割
- 10.卡尔曼滤波之次优滤波器——常增益和解耦
- 电商进入“后补贴时代”,为什么玩家集体做起长期投入?
- 赏金传奇【全自动】辅助脚本
- 深度学习——全连接层(Fully connected dence layers)原理解析
- 一种替代印象笔记的方法
- poj 3243:A Simple Problem with Integers
- JavaWeb项目调用QQ登录----QQ互联
- SpringIOC的配置及使用
- docker compose 部署 NGINX + PHP+MySQL