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的更多相关文章

  1. 滑动窗口解决Substring Search Problem

    2018-07-18 11:19:19 一.Minimum Window Substring 问题描述: 问题求解: public String minWindow(String s, String ...

  2. [Leetcode 3] 最长不重复子串 Longest substring without repeating 滑动窗口

    [题目] Given a string, find the length of the longest substring without repeating characters. [举例] Exa ...

  3. POJ 3320 Jessica's Reading Problem (滑动窗口)

    题意:给定一个序列,求一个最短区间,使得这个区间包含所有的种类数. 析:最近刚做了几个滑动窗口的题,这个很明显也是,肯定不能暴力啊,时间承受不了啊,所以 我们使用滑动窗口来解决,要算出所有的种数,我用 ...

  4. leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)

    用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstri ...

  5. [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 ...

  6. lintcode 滑动窗口的最大值(双端队列)

    题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为  ...

  7. CodeForces 701C They Are Everywhere (滑动窗口)

    题目链接:http://codeforces.com/problemset/problem/701/C 题意:找到字符串中能包含所有元素的最短字符串长度. 利用“滑动窗口”解题 解题思路: 1. 遍历 ...

  8. hdu-5497 Inversion(滑动窗口+树状数组)

    题目链接: Inversion Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  9. [CF580B]Kefa and Company(滑动窗口)

    题目链接:http://codeforces.com/problemset/problem/580/B 某人有n个朋友,这n个朋友有钱数m和关系s两个属性.问如何选择朋友,使得这些朋友之间s最大差距小 ...

随机推荐

  1. 《人性与暴力-论暴力侵略》ER

    card level 人名卡 反常识卡 世界发生了什么? - 信息爆炸 信息与信任构成了人类发展两大命题 人性史与暴力史 未来世界会变得怎么样?- 历史不会重复,但会押韵.暴力呈现下降. 改变世界的三 ...

  2. 每日一译系列-模块化css怎么玩(译文)

    原文链接:How Css Modules Work 原文作者是Preact的作者 这是一篇关于如何使用Css Modules的快速介绍,使用到的工具是Webpack吊炸的css-loader 首先,我 ...

  3. 泰拉瑞亚Linux主机打造指南

    最近玩了泰拉瑞亚,一个2D版的我的世界,但苦于steam的联机太过不靠谱,经常会出现和朋友之间联机失败的问题,所以我把服务器放到了部署我博客的服务器,这样就可以通过IP直接让好友加入游戏了! 首先是购 ...

  4. sql -- 多表关联,update(用户奖励)

    表设计: users_buy: users_score: 需求: 1.根据用户分组,找出用户消费最高的金额 select user_name, max(paymoney) as pm from use ...

  5. 统计 Django 项目的测试覆盖率

    作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 我们完成了对 blog 应用和 comment 应用这两个核心 app 的测试.现在 ...

  6. py装饰器,生成器,迭代器

    emmmmm....看了好久才能大概的看懂一点关于装饰器的内容...import sys # 引入sys模块import timeimport functoolsfrom functools impo ...

  7. python从一个目录中复制全部文件图片至另一个目录中,及删除指定目录中的图片

    import shutil import os #目录自己改一下即可,复制 path = "./static/imgs/" new_path = "./static/up ...

  8. 简单服务器端Blazor Cookie身份验证的演示

    为了演示身份验证如何在服务器端 Blazor 应用程序中工作,我们将把身份验证简化为最基本的元素. 我们将简单地设置一个 cookie,然后读取应用程序中的 cookie. 应用程序身份验证 大多数商 ...

  9. java 发送邮件详细讲解

    一.JavaMail概述:        JavaMail是由Sun定义的一套收发电子邮件的API,不同的厂商可以提供自己的实现类.但它并没有包含在JDK中,而是作为JavaEE的一部分. 厂商所提供 ...

  10. Design Patterns | 02 什么样的代码是好代码

    目录 01 - 什么是好的代码? 02 - 评价代码的标准有哪些 2.1 可维护性(maintainability) 2.2 可读性(readability) 2.3 可扩展性(extensibili ...