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

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字符集与乱码(转)

    作者:hcling97    http://blog.sina.com.cn/hcling97 2013年5月15日 转载请注明出处 字符集问题一直叫人头疼,究其原因还是不能完全明白其运作原理. 在整 ...

  2. QTextEdit字符串的高亮显示问题

    20130222 鬼猫猫 整理 http://www.cnblogs.com/muyr/ 解决方法的原始地址 http://www.qtcn.org/bbs/read.php?tid=20335 背景 ...

  3. Docker镜像仓库Harbor安装

    export VERSION=18.06 && curl -fsSL http://rainbond-pkg.oss-cn-shanghai.aliyuncs.com/releases ...

  4. 圣诞快乐!OIer挂分小技巧

    OIer常犯错误 自己的错误 循环里套return 线段树求和 int 定义,下传 int 定义 cmp<,>号分不清 主观行为举动错误 踢电源线,注意安全(_Destiny) TLE 大 ...

  5. 精通MySQL之索引篇,这篇注重练习!

    老刘是即将找工作的研究生,自学大数据开发,一路走来,感慨颇深,网上大数据的资料良莠不齐,于是想写一份详细的大数据开发指南.这份指南把大数据的[基础知识][框架分析][源码理解]都用自己的话描述出来,让 ...

  6. Java AQS的原理

    首先可以看:  http://ifeve.com/java-special-troops-aqs/ 再看  <Java并发编程的艺术>第5章 核心:同步器的acquire方法: 首先调用自 ...

  7. java.util.List.subList使用注意

    List<E> subList(int fromIndex, int toIndex); 它返回原来list的从[fromIndex, toIndex)之间这一部分的视图,之所以说是视图, ...

  8. flutter--Dart基础语法(一)

    一.前言 Flutter 是 Google 开源的 UI 工具包,帮助开发者通过一套代码库高效构建多平台精美应用,Flutter 开源.免费,拥有宽松的开源协议,支持移动.Web.桌面和嵌入式平台. ...

  9. Nginx 指定域名(或子域名)和网站绑定

    问题起因 博主最近在 CentOS 上面部署另外一个网站,但并不想通过端口号来访问,因为端口号对于 SEO 优化不利,且用户访问较繁琐(使用域名不就是为了方便用户访问吗?再引入端口号岂不是和使用域名的 ...

  10. mysql高级day1

    Mysql高级-day01 MySQL高级课程简介 序号 Day01 Day02 Day03 Day04 1 Linux系统安装MySQL 体系结构 应用优化 MySQL 常用工具 2 索引 存储引擎 ...