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

LeetCode Hot 100:滑动窗口

LeetCode Hot 100:滑动窗口

3. 无重复字符的最长子串

思路 1:滑动窗口 + 哈希表

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n = s.length();
        int maxLen = 0;
        unordered_set<char> hashSet;
        for (int left = 0, right = 0; right < n; right++) {
            while (hashSet.count(s[right])) {
                hashSet.erase(s[left]);
                left++;
            }
            hashSet.insert(s[right]);
            maxLen = max(maxLen, right - left + 1);
        }
        return maxLen;
    }
};

思路 2:动态规划 + 哈希表

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n = s.length();
        unordered_map<char, int> dic;
        int res = 0, tmp = 0;
        for (int left, right = 0; right < n; right++) {
            if (dic.find(s[right]) == dic.end())
                left = -1;
            else
                left = dic.find(s[right])->second; // 获取索引 i
            // 更新哈希表
            dic[s[right]] = right;
            // dp[j - 1] -> dp[j]
            tmp = tmp < right - left ? tmp + 1 : right - left;
            res = max(res, tmp);
        }
        return res;
    }
};

438. 找到字符串中所有字母异位词

思路 1:固定长度滑动窗口 + 哈希表

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int sLen = s.length(), pLen = p.length();
        if (sLen < pLen)
            return {};

        vector<int> sCount(26, 0), pCount(26, 0);
        vector<int> ans;

        for (int i = 0; i < pLen; i++) {
            sCount[s[i] - 'a']++;
            pCount[p[i] - 'a']++;
        }

        if (sCount == pCount)
            ans.push_back(0);

        for (int i = pLen; i < sLen; i++) {
            sCount[s[i - pLen] - 'a']--;
            sCount[s[i] - 'a']++;
            if (sCount == pCount)
            {
                // 子串开头是 i - pLen + 1
                ans.push_back(i - pLen + 1);
            }
        }
        return ans;
    }
};

思路 2:可变长度滑动窗口 + 哈希表

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int sLen = s.length(), pLen = p.length();
        if (sLen < pLen)
            return {};

        vector<int> sCount(26, 0), pCount(26, 0);
        vector<int> ans;

        for (int i = 0; i < pLen; i++)
            pCount[p[i] - 'a']++;

        if (sCount == pCount)
            ans.push_back(0);

        for (int left = 0, right = 0; right < sLen; right++) {
            sCount[s[right] - 'a']++;
            while (sCount[s[right] - 'a'] > pCount[s[right] - 'a']) {
                sCount[s[left] - 'a']--;
                left++;
            }
            if (right - left + 1 == pLen)
                ans.push_back(left);
        }

        return ans;
    }
};

http://www.kler.cn/news/361331.html

相关文章:

  • Self-Operating Computer Framework项目部署日记
  • idea(2017版)创建项目的搭建方式
  • 第23章 - Elasticsearch 洞悉你的查询:如何在上线前发现潜在问题!
  • C#第6讲:集合字典
  • 【JPCS独立出版 | 福州大学主办 | 有确定的ISSN号】第三届可再生能源与电气科技国际学术会议(ICREET 2024)
  • 第十四章_File类与IO流
  • Python爬虫:API接口数据的“美食侦探”
  • 如何将markdown文件转换为pdf
  • flutter camera 插件相机不占满屏幕的问题
  • HDU The Boss on Mars(容斥原理)
  • DevExpress WPF v24.1新版亮点:PDF查看器、富文本编辑器功能升级
  • HTTP: GET vs POST
  • git安装与使用的史诗级教程【git推送文件到远程仓库(GitHub)教程】
  • 基于知识图的电影推荐系统
  • 记录一下乐鑫官方仓库ESP32-CAMERA,在使用esp32s3 wroom n16r8 CAM开发板时遇到的问题。
  • 刷题小记9:回溯
  • to_sql报错not all arguments converted during string formatting
  • 5.redis安装【Docker】
  • vscode 预览markdown 文件
  • 【C++干货篇】——类和对象的魅力(四)