当前位置: 首页 > news >正文

leetcode:最小覆盖字符串

1笨方法

对于算法题目,自己能想到的往往是最基础的笨方法。

代码如下:

如果t的长度是len1,s的长度是len2,那么最小窗口是len1,最大窗口是len2。所以可以从len1到len2,遍历窗口大小,对于每个窗口大小,将窗口从前向后进行移动。因为窗口是从小到大进行遍历,所以第一次遇到满足条件的子串时,就可以直接范围。这种算法的时间复杂度是O(n*n),在leetcode上运行会超时。

class Solution {
public:string minWindow(string s, string t) {std::map<char, int> tConstMap;std::map<char, int> tTmpMap;for (char c : t) {if (tConstMap.count(c) > 0) {tConstMap[c]++;} else {tConstMap[c] = 1;}tTmpMap[c] = 0;}int minWindow = t.size();int maxWindow = s.size();int maxLength = s.size();for (int window = minWindow; window <= maxWindow; window++) {for (int i = 0; i <= maxLength - window; i++) {for (int j = i; j < i + window; j++) {if (tConstMap.count(s[j]) > 0) {tTmpMap[s[j]]++;}}bool result = true;for (auto [c, count] : tTmpMap) {if(count < tConstMap[c]) {result = false;}tTmpMap[c] = 0;}if (result == true) {std::cout << "i:" << i <<",window:" << window <<std::endl;return s.substr(i, window);}}}return "";}
};

 针对上边的代码,我们可以看到,针对一个确定大小的window,在从前向后遍历的过程中,每次只移动了一个字符的位置,但是每次都要对tTmpMap进行清空并重新计算,完全可以只对移出窗口的元素和移入窗口的元素进行处理。但是时间复杂度仍然较高,在leetcode上运行会超时。

class Solution {
public:string minWindow(string s, string t) {std::map<char, int> tConstMap;std::map<char, int> tTmpMap;for (char c : t) {if (tConstMap.count(c) > 0) {tConstMap[c]++;} else {tConstMap[c] = 1;}tTmpMap[c] = 0;}int minWindow = t.size();int maxWindow = s.size();int maxLength = s.size();for (int window = minWindow; window <= maxWindow; window++) {for (int i = 0; i <= maxLength - window; i++) {if (i == 0) {for (auto [c, value] : tTmpMap) {tTmpMap[c] = 0;}for (int j = i; j < i + window; j++) {if (tConstMap.count(s[j]) > 0) {tTmpMap[s[j]]++;}}} else {if (tConstMap.count(s[i +window - 1]) > 0) {tTmpMap[s[i +window - 1]]++;}}bool result = true;for (auto [c, count] : tTmpMap) {if(count < tConstMap[c]) {result = false;break;}}if (result == true) {return s.substr(i, window);}if (tTmpMap.count(s[i]) > 0) {tTmpMap[s[i]]--;}}}return "";}
};

2leetcode题解一

官方题解中没有遍历窗口的大小,而是使用双指针的方式。用窗口的左边沿和右边沿来表示一个窗口,通过左右边沿的移动来遍历不同的情况。

class Solution {
public://t中字符出现的次数unordered_map <char, int> ori; //s中字符出现的次数unordered_map <char, int> cnt;//判断当前子串是不是覆盖了tbool check() {for (const auto &p: ori) {if (cnt[p.first] < p.second) {return false;}}return true;}string minWindow(string s, string t) {//t中字符出现的次数for (const auto &c: t) {ori[c]++;}int l = 0;//窗口左边沿int r = 0;//窗口右边沿int len = INT_MAX;int ansL = -1;int ansR = -1;while (r < int(s.size())) {//统计s中字符的数量,移动窗口右边沿if (ori.find(s[r]) != ori.end()) {cnt[s[r]]++;}//移动窗口左边沿,找到慢去条件的最小窗口while (check() && l <= r) {if (r - l + 1 < len) {len = r - l + 1;ansL = l;}if (ori.find(s[l]) != ori.end()) {cnt[s[l]]--;}l++;}r++;}return ansL == -1 ? string() : s.substr(ansL, len);}
};

3leetcode题解二

题解二比题解一性能更高。

相同点:

①都维护了两个map,分别用于记录s和t中字符出现的次数。

②窗口右边沿的移动均是沿着s进行移动,没有其它的条件约束。


不同点:

①题解二中多了一个count,用count和t的长度是否相等来表示当前子串是不是覆盖了t。当s中的字符出现次数少于t中字符的出现次数时,对count进行递增。而题解一种使用函数check来判断当前子串是不是覆盖了t。

②窗口左边沿的移动方式不同:题解一种移动左边沿,加上check函数进行判断,比较好理解;题解二中移动左边界,是当当前字符出现的次数比t中出现的次数多的时候,才会像猴移动(如果字符在t中没有出现,在s中出现了,那么会移动;如果字符在s和t中都出现了,并且在s中出现的次数比t中多,那么也可以移动)。

class Solution {
public:string minWindow(string s, string t) {for (char c : t) {ht[c]++;}for (int i = 0, j = 0; i < s.size(); i++) {hs[s[i]]++;if (hs[s[i]] <= ht[s[i]]) {count++;}while (hs[s[j]] > ht[s[j]]) {hs[s[j]]--;j++;}if (count == t.size()) {if (ret.empty() || i - j + 1 < ret.size()) {ret = s.substr(j, i - j + 1);}}}return ret;}private:int hs[123] = {0};int ht[123] = {0};int count = 0;std::string ret = "";
};

相关文章:

  • LeetCode 1007. 行相等的最少多米诺旋转 题解
  • php study 网站出现404 - Page Not Found 未找到
  • 深度学习中的数据增强:提升食物图像分类模型性能的关键策略
  • VTK入门指南
  • [三分钟学算法]分治-快速排序-最小的K个数:设计一个算法,找出数组中最小的k个数。以任意顺序返回这k个数均可。
  • 【数据结构】稀疏矩阵的快速转置
  • 架构思维:异构数据的同步一致性方案
  • P1802 5 倍经验日
  • 递归算法详解(Java 实现):从原理到高阶应用
  • 时序分解 | Matlab基于WOA-MVMD鲸鱼算法优化多元变分模态分解
  • 机器学习实操 第二部分 神经网路和深度学习 第11章 训练深度神经网络
  • GenCLS++:通过联合优化SFT和RL,提升生成式大模型的分类效果
  • 人工智能数学基础(八)—— 最优化理论
  • 生物化学笔记:神经生物学概论09 小脑的运动学习 基底神经节的结构与功能
  • C++八股--6--mysql 日志与并发控制
  • 永磁同步电机无速度算法--基于ESO-PLL的永磁同步电机无位置传感器控制
  • 2025年PMP 学习二
  • 第一章 - 质量
  • C++学习:六个月从基础到就业——C++11/14:右值引用与移动语义
  • Docker安装Gitblit(图文教程)
  • 加拿大总理将赴美同特朗普会晤,重点谈贸易压力
  • 2024年境内酒店住宿行业指标同比下滑:酒店行业传统增长模式面临挑战
  • 玉渊谭天:美方多渠道主动接触中方希望谈关税
  • 美国第一季度经济环比萎缩0.3%,特朗普:怪拜登,与关税无关
  • 共绘“彩色上海”,IP SH艺术共创沙龙首期圆满举办
  • 李乐成任工业和信息化部部长