LeetCode 算法:字符串匹配、最长无重复字符子串长度

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 算法:字符串匹配、最长无重复字符子串长度相关的知识,希望对你有一定的参考价值。

参考技术A

Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
笔记:
类似第1题的思路,但是这里使用列表代替字典,记录当前找到的无重复字符。用列表的好处是它是有序的,且支持随机访问。

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
笔记:
采用了简单匹配算法
改进:KMP算法

《LeetCode之每日一题》:78.无重复字符的最长字串

无重复字符的最长字串


题目链接: 无重复字符的最长字串

有关题目

题解

方法一:滑动窗口

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // 哈希集合,记录每个字符是否出现过
        unordered_set<char> occ;
        int n = s.length();
        // 右指针rk,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
        int rk = -1, ans = 0;
        //左指针 i
        for (int i = 0; i < n; i++){
        
            if (i != 0){
                // 左指针向右移动一格,移除一个字符
                occ.erase(s[i - 1]);
            }
            while(rk + 1 < n && !occ.count(s[rk + 1])){
                // 不断地移动右指针
                occ.insert(s[rk + 1]);
                rk++;
            }
            ans = max(ans, rk - i + 1);
            // 第 i 到 rk 个字符是一个极长的无重复字符子串
        }
        return ans;
    }
};

代码优化:

1.
//当已知最长的长度超过剩下的字符串的长度是,我们直接break;
            if (ans >= n - i ){
                break;
            }
2.
//外层循环中,左指针一直移动到导致内层循环结束的那个对应字符(如:.ab.cb(.从a之b.之后
           // 前))的后一位
            while(rk < n && occ.count(s[rk + 1])){
                occ.erase(s[i]);
                i++;
            }
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // 哈希集合,记录每个字符是否出现过
        unordered_set<char> occ;
        int n = s.length();
        // 右指针rk,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
        int rk = -1, ans = 0;
        //左指针 i
        int i = 0;
        while(i < n){
            //当已知最长的长度超过剩下的字符串的长度是,我们直接break;
            if (ans >= n - i ){
                break;
            }
            while(rk + 1 < n && !occ.count(s[rk + 1])){
                // 不断地移动右指针
                occ.insert(s[rk + 1]);
                rk++;
            }
            ans = max(ans, rk - i + 1);// 第 i 到 rk 个字符是一个极长的无重复字符子串
            
            //外层循环中,左指针一直移动到导致内层循环结束的那个对应字符(如:.ab.cb(.从a之b.之后
           // 前))的后一位
            while(rk + 1 < n && occ.count(s[rk + 1])){
                occ.erase(s[i]);
                i++;
            }
        }
        return ans;
    }
};

在这里插入图片描述

以上是关于LeetCode 算法:字符串匹配、最长无重复字符子串长度的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode]3无重复字符的最长字串

《LeetCode之每日一题》:78.无重复字符的最长字串

LeetCode3:最长不重复字串

leetcode第三题 无重复的最长字串

无重复字符的最长字串问题

编程练习:无重复字符的最长字串