LeetCode-76-Minimum Window Substring
发布时间:2020-12-14 02:33:05 所属栏目:Windows 来源:网络整理
导读:算法描述: 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). Example: Input: S = "ADOBECODEBANC",T = "ABC"Output: "BANC" Note: If there is no such window in S that c
算法描述: 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). Example: Input: S = "ADOBECODEBANC",T = "ABC" Output: "BANC" Note:
解题思路:子字符串问题,可以采用强大的滑动窗口方法解决。首先用map对字符串进行映射,并用两个指针分别指向窗口左边和右边,通过不断的滑动两个指针实现对窗口内字符的操作。 string minWindow(string s,string t) { string res = ""; unordered_map<char,int> map; for(char c:t) map[c]++; int left=0; int right = 0; int distance = INT_MAX; int count = t.size(); int head = 0; while(right < s.size()){ if(map[s[right++]]-- >0) count--; while(count==0){ if(right-left< distance){ head = left; distance = right-head; } if(map[s[left++]]++ ==0) count++; } } return distance==INT_MAX?"":s.substr(head,distance); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在另一个appdomain中加载dll的配置文件
- 我应该在哪里将ack配置文件放在Windows上?
- windows server 2012配置php环境教程
- 需要一步一步的WCF作为Windows服务
- windows – 我的NIC ID是什么?
- 重复Microsoft Word VBA,直到找不到搜索结果
- Windows10 & Python3 部署Robot FrameWork自动化测试框
- windows – 为通过USB连接的某些硬件编写游戏控制器驱动程序
- iis-7 – Powershell Get-WebSite名称参数被忽略
- Windows Phone Mango – ListBox还是LongListSelector?
推荐文章
站长推荐
- 深度学习番外篇---WIN10+PyTorch0.4.0+CUDA9.1环
- windows-server-2012 – 如何修复此Windows 2012
- ExtJS打开窗口,最大高度
- 需要帮助在Windows上为python27构建调试lxml
- 如何将软件注册为Windows 7的防病毒软件?
- Windows – 尝试不同文件的asmx应用程序的服务器
- 适用于Windows的可移植C/C++应用程序的DLL依赖项
- 如何在Windows 2003 Server中获取激活密钥
- windows-server-2003 – 如何手动运行Windows 20
- windows-server-2008-r2 – Windows Server 2008
热点阅读