当前位置:网站首页>Leetcode每日一题30. 串联所有单词的子串
Leetcode每日一题30. 串联所有单词的子串
2022-07-27 05:21:00 【最后一只三脚兽】
移动窗口,花了很久,AC以后,高歌一首。。。
思路
一眼看上去就是移动窗口,重点在于窗口的初始化以及记录在移动窗口过程中所需单词数目的改变
初始化:
对于该题一次移动窗口是不够的,由于窗口是按照单词距离移动,所以需要从开始单词的每一个字符都初始化一个窗口,共初始化words[0].length()个窗口窗口的移动:
首先用num记录所需单词的总数,即word.length,在移动的过程中我们不用在意细节,只要一知道被移除的单词是否是所需单词,如果是所需单词并且该单词在窗口中出现的数量小于等于所需该单词的数量就把num++;二知道新来的单词是否是所需单词,如果是并且该单词在窗口中出现的数量小于等于所需该单词的数量,就把num–。每次移入一个所需单词看num是否为0即可
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
HashMap<String,Integer> cnt = new HashMap<>();
List<Integer> ans = new ArrayList<>();
int w = words[0].length();
for(int i=0;i<words.length;i++){
cnt.put(words[i],cnt.getOrDefault(words[i], 0) + 1);
}
for(int i=0;i<w;i++){
HashMap<String,Integer> m= new HashMap<>();
int num = words.length;//记录还需要在窗口内的单词数
m.putAll(cnt);
int l = i;
int r = i + w * words.length-1;
if(r > s.length() - 1)break;//初始的窗口装不下所有单词直接结束
//滑动窗口初始化
for(int j=l;j<=r && j+w-1<=r;j+=w){
String cur = s.substring(j,j+w);
if(!m.containsKey(cur))continue;
int tmp = m.get(cur);
m.put(cur,tmp - 1);
if(tmp - 1 >= 0) {
num--;
}
}
if(num == 0){
//测试初始窗口是否满足要求
ans.add(i);
}
//移动滑动窗口
for (l+=w,r+=w; r<s.length(); l+=w,r+=w){
String old = s.substring(l-w,l);
String cur = s.substring(r-w+1,r+1);
//处理移出窗口的单词
if(m.containsKey(old)){
int tmp = m.get(old);
if(tmp >= 0) {
num++;
}
m.put(old,tmp + 1);
}
//处理刚进入窗口的单词
if(!m.containsKey(cur))continue;
int tmp = m.get(cur);
m.put(cur,tmp - 1);
if(tmp - 1 >= 0) {
num--;
}
if(num == 0){
ans.add(l);
}
}
}
return ans;
}
}
- 时间复杂度(N*M)N为单词长度,M为字符串长度
- 空间复杂度(N)哈希表
边栏推荐
- 发布 分辨率0.22m的建筑物分割数据库
- 【头歌】重生之我在py入门实训中(2):公式编程
- 【头歌】重生之数据科学导论——回归进阶
- Weidongshan digital photo frame project learning (II) displaying Chinese characters on LCD
- WebODM win10安装教程(亲测)
- 编程学习记录——第9课【操作符】
- Lightroom Classic 2022 v11.4中文版「最新资源」
- [first song] rebirth of me in py introductory training (4): Circular program
- 使用-Wall清除代码隐患
- 子类调用父类构造函数的时机
猜你喜欢

9. High order operation

浅记一下十大排序

Auto Encoder(AE),Denoising Auto Encoder(DAE), Variational Auto Encoder(VAE) 区别

西瓜书第三章---线性模型学习笔记

Multi task foundation of IOT operating system
![[Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog](/img/75/8f41db9f9c077b43751d63b7b5b57e.png)
[Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog

8. Mathematical operation and attribute statistics

Pix2Pix原理解析

pytorch使用data_prefetcher提升数据读取速度

【头歌】重生之机器学习-线性回归
随机推荐
malloc和new之间的不同-实战篇
VSCode解决运行ipynb文件使用卡顿问题
[first song] rebirth of me in py introductory training (3): if conditional sentence
数据库索引的一些说明以及使用
What tools are needed to make video post effects?
编程学习记录——第3课【初识C语言】
socket编程二:使用select
STM32 infrared remote control
关于druid连接不上数据库的问题
Weidongshan digital photo frame project learning (II) displaying Chinese characters on LCD
Dpdk network protocol stack VPP OVS DDoS Sdn nfv virtualization high performance expert Road
arcgis for js api-入门系列
[song] rebirth of me in py introductory training (12): Matplotlib interface and common graphics
【头歌】重生之我在py入门实训中(4):循环程序
cycleGAN解析
[first song] rebirth of me in py introductory training (4): Circular program
Speech and Language Processing (3rd ed. draft) Chapter 2 ——正则表达式,文本归一化,编辑距离 阅读笔记
[song] rebirth of me in py introductory training (7): function call
文件的路径
剪枝-量化-转onnx中文系列教程

