力扣76. 最小覆盖子串(滑动窗口)

news/2025/3/14 18:01:39/

Problem: 76. 最小覆盖子串

文章目录

  • 题目描述
  • 思路
  • 复杂度
  • Code

题目描述

在这里插入图片描述在这里插入图片描述

思路

1.定义两个map集合need和window(以字符作为键,对应字符出现的个数作为值),将子串t存入need中;
2.定义左右指针left、right均指向0(形成窗口),定义int类型变量len记录最小窗口长度,valid记录当前窗口否存最短子串中的字符个数
3.向右扩大窗口,遍历到到的字符c如果在need中时,window[c]++,同时如果window[c] == need[c],则valid++
4.如果valid == need.size(),则表示可以开始收缩窗口,并更新最小窗口禅读(如果移除的字符在need中,同时window[d] == need[d],则valid–,window[d]–);

复杂度

时间复杂度:

O ( n ) O(n) O(n);其中 n n n为字符串 s s s的长度

空间复杂度:

O ( n ) O(n) O(n)

Code

class Solution {
public:/*** Two pointer** @param s Given string* @param t Given string* @return string*/string minWindow(string s, string t) {unordered_map<char, int> need;unordered_map<char, int> window;for (char c: t) {need[c]++;}int left = 0;int right = 0;int valid = 0;// Records the starting index and length of the minimum overlay substringint start = 0;int len = INT_MAX;while (right < s.size()) {//c is the character moved into the windowchar c = s[right];// Move the window rightright++;// Perform some column updates to the data in the windowif (need.count(c)) {window[c]++;if (window[c] == need[c]) {valid++;}}// Determine whether to shrink the left windowwhile (valid == need.size()) {// Update the minimum overlay substringif (right - left < len) {start = left;len = right - left;}//d is the character to be moved out of the windowchar d = s[left];// Move the window leftleft++;// Perform some column updates to the data in the windowif (need.count(d)) {if (window[d] == need[d]) {valid--;}window[d]--;}}}// Returns the minimum overlay substringreturn len == INT_MAX ? "" : s.substr(start, len);}
};

http://www.ppmy.cn/news/1371883.html

相关文章

Vue组件数据双向绑定 v-model

v-model可以在组件上使用以实现双向绑定。 Vue 3.4之前使用方式 1、props & emit 父组件 <template><Child v-model"name"v-model:first-name"firstN"v-model:last-name"last"/> </template>子组件 <script setup…

GCN原理回顾

Cora_dataset description Cora数据集是一个常用的学术文献用网络数据集&#xff0c;用于研究学术文献分类和图网络分析等任务。 该数据集由机器学习领域的博士论文摘要组成&#xff0c;共计2708篇论文&#xff0c;涵盖了7个不同的学科领域。每篇论文都有一个唯一的ID&#xf…

AI大模型的预训练、迁移和中间件编程

大家好&#xff0c;我是爱编程的喵喵。双985硕士毕业&#xff0c;现担任全栈工程师一职&#xff0c;热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。…

redis10 应用问题(穿透、击穿、雪崩、分布式锁)

思维草图 缓存穿透 查询不存在的数据&#xff0c;穿透redis缓存&#xff0c;请求直接攻击后端db。 问题 当系统中引入redis缓存后&#xff0c;一个请求进来后&#xff0c;会先从redis缓存中查询&#xff0c;缓存有就直接返回&#xff08;相当于一道隔离闸&#xff0c;保护db…

微擎安装,卡在“安装微擎”界面

进入install.php&#xff0c;点击【在线安装】 下一步配置数据库&#xff0c;开始安装系统 然后显示进度条&#xff0c;进度条一闪而过 然后就没有进度条显示了&#xff0c;一直卡在这里 第一次等了好久&#xff0c; 删除目录下的文件&#xff0c;重装还是这样 再重启服务器&…

Spring Boot文档目录

目录 官方文档 说明&#xff1a;本文档翻译的版本&#xff1a;2.7.18-SNAPSHOT。 1. 法规&#xff08;Legal&#xff09; 2. 获取帮助&#xff08;Getting Help&#xff09; 3. 文档概述&#xff08;Documentation Overview&#xff09; 4. 开始使用&#xff08;Getting Sta…

抖音小店简洁版运营流程,帮助新手商家快速学习!

大家好&#xff0c;我是电商糖果 一个做了7年电商的90后&#xff0c;从2020年开始做抖音小店&#xff0c;现在已经经营了多家小店。 这篇文章从开店到选品&#xff0c;出体验分&#xff0c;找达人合作&#xff0c;对接厂家等全部给分享出来。 一、准备材料 1. 个体户营业执…

docker安装php7.4安装(swoole)

容器 docker pull centos:centos7 docker run -dit -p9100:9100 --name“dade” --privilegedtrue centos:centos7 /usr/sbin/init 一、安装前库文件和工具准备 1、首先安装 EPEL 源 yum -y install epel-release2.安装 REMI 源 yum -y install http://rpms.remirepo.net/en…