Leetcode 30 串联所有单词的子串 滑动窗口+map
见注释。滑动窗口还是好用。
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的更多相关文章
- Java实现 LeetCode 30 串联所有单词的子串
30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...
- [LeetCode] 30. 串联所有单词的子串
题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...
- [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 ...
- [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 ...
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...
- Leetcode 30.与所有单词相关联的子串
与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...
- [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)
30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...
- Leetcode——30.与所有单词相关联的字串【##】
@author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一 ...
- leetcode的Hot100系列--3. 无重复字符的最长子串--滑动窗口
可以先想下这两个问题: 1.怎样使用滑动窗口? 2.如何快速的解决字符查重问题? 滑动窗口 可以想象一下有两个指针,一个叫begin,一个叫now 这两个指针就指定了当前正在比较无重复的字符串,当再往 ...
随机推荐
- 一种获取context中keys和values的高效方法 | golang
我们知道,在 golang 中的 context 是一个非常重要的包,保存了代码活动的上下文.我们经常使用 WithValue() 这个方法,来往 context 中 传递一些 key value 数 ...
- Effective Java, 3e阅读笔记一
引言 本书的目标是帮助读者更加有效地使用Java编程语言及其基本类库,适用于任何具有实际Java工作经验的程序员. 本书一共90个条目,12章,每个条目讨论一条规则,这些规则反映了最有经验的优秀程序员 ...
- Vue基础之Vue组件
Vue基础之Vue组件 // 组件是可以复用的Vue实例! // 可以把经常重复的功能封装为组件!
- 电脑微信电脑PC 多开/防撤回 补丁
简介 经常使用微信电脑版的用户都会发现一个蛋疼的事情,那就是微信PC版不支持多开,也就是不能同时登陆多个账号,这对于需要在电脑上登陆多个微信账号的朋友来说肯定是极其的不方便.另外有的时候别人撤回了一些 ...
- WPF combobox设置默认选项不生效的问题
combobox 是常用的控件,当我们需要绑定设置默认选项时,往往会绑定 SelectedItem 属性去设置, 可是你会惊奇地发现SelectedItem的值绑定了, 它依旧是熟悉的模样 根据官方的 ...
- CF976B
这是一道考验思维找规律的题,很有可做性. 正文 题意 一个 n * m 的矩阵,从左上角(1 , 1) 开始,先向下走直到最下方,再向右走到最右,再向上走一个,再走到最左......一直走到(1 , ...
- ThinkPHP3.2.4 order方法注入
漏洞详情: 漏洞文件:./ThinkPHP\Library\Think\Db\Driver.class.php 中的 parseOrder方法: 这也是继上次order方法注入之后的修复手段. 可以看 ...
- 2021年,python的入门基础-----基础一
先记录下pycharm编译器相关的信息 1.某些常用快捷键: Ctrl+/ 注释: Tab缩进,shift+Tab; Ctrl+Z 撤销 2.设置界面编辑风格: File>Settings> ...
- Cisco静态路由
怎样让身在两个网段的终端会话交流呢?我们借用Cisco packet研究一下Cisco静态路由. 名词解释: 网关:(Gateway)网间连接器,或叫协议转换器:举例(参考https://baike. ...
- 31-1.解决service iptables save出错
CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替.service命令只保留下了极少部分使用 ...