滑动窗口-Substring Search Problem
2018-07-18 11:19:19
一、Minimum Window Substring
问题描述:

问题求解:
public String minWindow(String s, String t) {
String res = "";
if (t.length() > s.length()) return res;
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < t.length(); i++) {
map.put(t.charAt(i), map.getOrDefault(t.charAt(i), 0) + 1);
}
int begin, end, count;
begin = 0;
end = 0;
count = map.size();
int minLen = Integer.MAX_VALUE;
for (; end < s.length(); end++) {
char c = s.charAt(end);
if (map.containsKey(c)) {
map.put(c, map.get(c) - 1);
if (map.get(c) == 0) count--;
}
while (count == 0) {
if (end - begin + 1 < minLen) {
res = s.substring(begin, end);
minLen = end - begin + 1;
}
char temp = s.charAt(begin);
if (map.containsKey(temp)) {
if (map.get(temp) == 0) count++;
map.put(temp, map.get(temp) + 1);
}
begin++;
}
}
return res;
}
二、Longest Substring Without Repeating Characters
问题描述:

问题求解:
public int lengthOfLongestSubstring(String s) {
int[] map = new int[256];
int res = 0;
int begin = 0;
for (int end = 0; end < s.length(); end++) {
char c = s.charAt(end);
map[c]++;
while (map[c] > 1) {
map[s.charAt(begin)]--;
begin++;
}
res = Math.max(res, end - begin + 1);
}
return res;
}
三、Substring with Concatenation of All Words
问题描述:

问题求解:
本题其实是一道拓展题,这里题目中给出了所有单词的长度相同,也就意味着可以将所有的单词看作单个字母来进行处理,如果是这样的话,那么外层循环很显然就是word length了。之后通过两个Map对单词进行计数就可以进行解决。
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new ArrayList<>();
if (s == null || s.length() == 0 || words.length == 0) return res;
Map<String, Integer> map = new HashMap<>();
int wl = words[0].length();
int num = words.length;
int n = s.length();
if (n < num * wl) return res;
for (int i = 0; i < num; i++)
map.put(words[i], map.getOrDefault(words[i], 0) + 1);
for (int i = 0; i < wl; i++) {
int begin, end, cnt;
Map<String, Integer> seen = new HashMap<>();
begin = i;
cnt = 0;
for (end = begin; end <= n - wl; end += wl) {
String str = s.substring(end, end + wl);
// 匹配成功
if (map.containsKey(str)) {
seen.put(str, seen.getOrDefault(str, 0) + 1);
cnt++;
// 若计数个数超过map, 则需要对窗口大小进行调整
while (seen.get(str) > map.get(str)) {
String temp = s.substring(begin, begin + wl);
seen.put(temp, seen.get(temp) - 1);
begin = begin + wl;
cnt--;
}
// 如果所有单词都匹配完成, 则向res中添加答案,同时将窗口大小向前挪动一步
if (cnt == num) {
res.add(begin);
String temp = s.substring(begin, begin + wl);
seen.put(temp, seen.get(temp) - 1);
begin = begin + wl;
cnt--;
}
}
// 当前匹配失败,则begin要从下一个开始,且所有的计数都要初始化
else {
seen.clear();
begin = end + wl;
cnt = 0;
}
}
}
return res;
}
四、Longest Substring with At Least K Repeating Characters
问题描述:

问题求解:
单纯的使用滑动窗口在修改窗口大小的时候会出现难以判断的情况,可以引入uniqueNum这个变量,来表征K个不同的字母,这样就可以大大降低问题的复杂度。
public int longestSubstring(String s, int k) {
int res = 0;
char[] chs = s.toCharArray();
for (int i = 1; i <= 26; i++) {
res = Math.max(res, helper(chs, k, i));
}
return res;
}
private int helper(char[] chs, int k, int uniqueNum) {
int res = 0;
int[] cnt = new int[256];
int begin = 0;
int nolessthank = 0;
int curUnique = 0;
for (int end = 0; end < chs.length; end++) {
if (cnt[chs[end]] == 0) curUnique++;
if (cnt[chs[end]] == k - 1) nolessthank++;
cnt[chs[end]]++;
while (curUnique > uniqueNum) {
if (cnt[chs[begin]] == k) nolessthank--;
if (cnt[chs[begin]] == 1) curUnique--;
cnt[chs[begin]]--;
begin++;
}
if (curUnique == uniqueNum && nolessthank == curUnique)
res = Math.max(res, end - begin + 1);
}
return res;
}
五、Max Consecutive Ones III
问题描述:

问题求解:
这种问题一般要么dp,要么滑动窗口,这种敏感性要有。
本题就是使用滑动窗口来进行解决的问题。实质上就是求解最长的substring其中至多包含k个0。
public int longestOnes(int[] A, int K) {
int res = 0;
int begin = 0;
int cnt = 0;
for (int end = 0; end < A.length; end++) {
if (A[end] == 0) cnt+= 1;
while (cnt > K) {
if (A[begin] == 0) cnt -= 1;
begin++;
}
res = Math.max(res, end - begin + 1);
}
return res;
}
滑动窗口-Substring Search Problem的更多相关文章
- 滑动窗口解决Substring Search Problem
2018-07-18 11:19:19 一.Minimum Window Substring 问题描述: 问题求解: public String minWindow(String s, String ...
- [Leetcode 3] 最长不重复子串 Longest substring without repeating 滑动窗口
[题目] Given a string, find the length of the longest substring without repeating characters. [举例] Exa ...
- POJ 3320 Jessica's Reading Problem (滑动窗口)
题意:给定一个序列,求一个最短区间,使得这个区间包含所有的种类数. 析:最近刚做了几个滑动窗口的题,这个很明显也是,肯定不能暴力啊,时间承受不了啊,所以 我们使用滑动窗口来解决,要算出所有的种数,我用 ...
- leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)
用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstri ...
- [LeetCode] Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- lintcode 滑动窗口的最大值(双端队列)
题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
- CodeForces 701C They Are Everywhere (滑动窗口)
题目链接:http://codeforces.com/problemset/problem/701/C 题意:找到字符串中能包含所有元素的最短字符串长度. 利用“滑动窗口”解题 解题思路: 1. 遍历 ...
- hdu-5497 Inversion(滑动窗口+树状数组)
题目链接: Inversion Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- [CF580B]Kefa and Company(滑动窗口)
题目链接:http://codeforces.com/problemset/problem/580/B 某人有n个朋友,这n个朋友有钱数m和关系s两个属性.问如何选择朋友,使得这些朋友之间s最大差距小 ...
随机推荐
- 1078 Hashing (25 分)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
- sql问题处理
批量杀死MySQL连接 select concat('KILL ',id,';') from information_schema.processlist where Info like 'selec ...
- 2016/11/10 吃吃喝喝Hacking Thursday Night聚餐活动 at Dunkin Donuts
店名:Dunkin Donuts 唐恩都乐 点评:http://www.dianping.com/shop/21378231 地址:静安区南京西路1649号静安公园内(近静安公园) 走法:地铁2号线静 ...
- Windows下python3登陆和操作linux服务器
一.环境准备 python3远程连接需要用到pycrytodome和paramiko库,其中后者依赖前者,所以按照顺序来安装 1. 安装pycrytodome 1 pip install pycryt ...
- nexus7入手
平板一直关注了很久了,关键是不知道平板对我来说,拿它来做什么用.所以,一直也就是关注,也没有决心买了. 终于这次出手了,N7,到货了! 照片是原生的android系统,不习惯,不习惯,直接用刷机精灵, ...
- 启动时查看配置文件application.yml
Spring Boot Application 事件和监听器 在多环境的情况下. 可能需要切换配置文件的一个对应的属性来切换环境 面临的问题就是 如何在springboot加载完配置文件的时候就可以立 ...
- Python神经网络编程笔记
神经元 想一想便知道,当一个人捏你一下以至于你会痛得叫起来的力度便是神经元的阈值,而我们构建的时候也是把这种现象抽象成一个函数,叫作激活函数. 而这里便是我们使用sigmoid函数的原因,它是一个很简 ...
- mac中如何复制拷贝文件或文件夹的路径
copy path in Mac 首先打开 automator(自动操作) 然后 最后command+s保存,命名为copy path. 然后随便找个文件右击,选择服务,再选择 copy path , ...
- Nginx之反向代理配置(二)
前文我们聊了Nginx的防盗链.反向代理以及开启nginx代理缓存,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12417130.html:今天我们继续说ng ...
- psql的时间类型,通过时间查询
psql的时间类型,通过时间查询 psql有date/timestamp类型,date只显示年月日1999-01-08,而timestamp显示年月日时分秒 1999-01-08 09:54:03.2 ...