见注释。滑动窗口还是好用。

class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int>res;
if(words.empty()||s.empty())
return res;
map<string,int>allWords;
int wordLen=words[0].size();
int wordNum=words.size();
for(int i=0;i<wordNum;i++)
{
if(allWords.find(words[i])!=allWords.end())
allWords[words[i]]++;
else
allWords.insert(make_pair(words[i],1));
} bool hasRemoved=false;
map<string, int> hasWords;
//将所有移动分成 wordLen 类情况 即从i=0,1,2...wordLen-1处开始,每次移动WordLen长度
for (int j = 0; j < wordLen; j++) {
hasWords.clear();
int num = 0; //记录当前hasWords中有多少个单词
for (int i = j; i +wordNum * wordLen< s.length()+1; i = i + wordLen) { //每次判断一个窗口,窗口从i开始长 wordNum * wordLen 窗口每次移动一个单词长度
hasRemoved = false; //防止情况三移除后,情况一继续移除
while (num < wordNum) {
string word = s.substr(i + num * wordLen,wordLen);
if (allWords.find(word)!=allWords.end()) {
if(hasWords.find(word)!=hasWords.end())
hasWords[word]++;
else
hasWords.insert(make_pair(word,1));
//遇到了符合的单词,但是次数超了
if (hasWords[word] > allWords[word]) {
hasRemoved = true;
int removeNum = 0;
//一直移除单词,直到次数符合了
while (hasWords[word] > allWords[word]) {
string firstWord = s.substr(i + removeNum * wordLen,wordLen);
hasWords[firstWord]-=1;
removeNum++;
}
num = num - removeNum + 1; //加 1 是因为我们把当前单词加入到了 HashMap 2 中
i = i + (removeNum - 1) * wordLen; //这里依旧是考虑到了最外层的 for 循环,看情况二的解释
break;
}
//出现情况二,遇到了不匹配的单词,直接将 i 移动到该单词的后边(但其实这里
//只是移动到了出现问题单词的地方,因为最外层有 for 循环, i 还会移动一个单词
//然后刚好就移动到了单词后边)
} else {
hasWords.clear();
i = i + num * wordLen;
num = 0;
break;
}
num++;
}
if (num == wordNum) {
res.push_back(i);
}
//出现情况一,子串完全匹配,我们将上一个子串的第一个单词从 HashMap2 中移除
if (num > 0 && !hasRemoved) {
string firstWord = s.substr(i,wordLen);
hasWords[firstWord]-=1;
num = num - 1;
} } }
return res;
}
};

Leetcode 30 串联所有单词的子串 滑动窗口+map的更多相关文章

  1. Java实现 LeetCode 30 串联所有单词的子串

    30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...

  2. [LeetCode] 30. 串联所有单词的子串

    题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...

  3. [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  4. [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  5. 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】

    [030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...

  6. Leetcode 30.与所有单词相关联的子串

    与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

  7. [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)

    30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...

  8. Leetcode——30.与所有单词相关联的字串【##】

    @author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一 ...

  9. leetcode的Hot100系列--3. 无重复字符的最长子串--滑动窗口

    可以先想下这两个问题: 1.怎样使用滑动窗口? 2.如何快速的解决字符查重问题? 滑动窗口 可以想象一下有两个指针,一个叫begin,一个叫now 这两个指针就指定了当前正在比较无重复的字符串,当再往 ...

随机推荐

  1. oracle创建恢复编录(recovery catalog)

    1.在要作为恢复编录的数据库创建用户 create user rman identified by oracle default tablespace system temporary TABLESP ...

  2. C# 关机/重启/注销计算机

    一.调用 shutdown.exe 执行操作 调用 shutdown.exe 执行计算机关机.重启.注销操作,还可以设置多长时间后执行操作,代码如下: 1 /// <summary> 2 ...

  3. 安装macosx10.13high serria

    本教程所需资源下载链接: 链接:https://pan.baidu.com/s/1wGTezXz6zGvtlwpv6mMoSg 提取码:r6n9 安装VMware workstation 16.0,安 ...

  4. Flask之路由系统

    路由系统 路由的两种写法 1.第一种方法: def index(): return render_template('index.html') app.add_url_rule('/index', ' ...

  5. canvas星空背景特效+CSS旋转相册学习

    今天在看帖子的时候,看到了个有趣的css旋转相册,刚好之前做了一个星空背景dome,这里给大家分享下代码: 旋转相册参考:https://blog.csdn.net/gitchatxiaomi/art ...

  6. 基于Redo Log和Undo Log的MySQL崩溃恢复流程

    在之前的文章「简单了解InnoDB底层原理」聊了一下MySQL的Buffer Pool.这里再简单提一嘴,Buffer Pool是MySQL内存结构中十分核心的一个组成,你可以先把它想象成一个黑盒子. ...

  7. 转 Fiddler2 下断点修改HTTP报文

    文章转自:https://www.cnblogs.com/zhengna/p/10861893.html 一 Fiddler中设置断点修改HTTP请求 方法1:全局断点.Rules-->Auto ...

  8. 初审blucms(入坑)

    作为一名初来乍到审计小白,从blueCMS入手再好不过了.通过对入门级的cms进行审计以及一个整体的框架和常见的漏洞学习,对个人而言是一次不错的学习经历.话不多说直接进入主题. 代码审计环境 Blue ...

  9. 前端面试之JavaScript中this的指向【待完善!】

    JavaScript中this的指向问题! 另一个特殊的对象是 this,它在标准函数和箭头函数中有不同的行为. 在标准函数中, this 引用的是把函数当成方法调用的上下文对象,这时候通常称其为 t ...

  10. YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. data = yaml.load(file_data)

    YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsa ...