leetcode Given a string s, please find the length hash set of the longest substring that does not contain repeated characters unordered_set C++

Given a string s, please find the length of the longest substring that does not contain repeated characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: Since the longest substring without repeating characters is "abc", its length is 3.
Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: Since the longest substring without repeating characters is "b", its length is 1.
Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: Since the longest substring without repeating characters is "wke", its length is 3.
Note that your answer must be the length of the substring, "pwke" is a subsequence, not a substring.

hint:

0 <= s.length <= 5 * 104
s consists of English letters, numbers, symbols and spaces

Source: LeetCode
Link: https://leetcode.cn/problems/longest-substring-without-repeating-characters
The copyright belongs to Leetcode Network. For commercial reprints, please contact the official authorization, for non-commercial reprints, please indicate the source.

answer analysis

The answer comes from the official website. The code added here is used to test the answer. I just analyze the logic of the code implementation here.

The code is mainly implemented by the hash set, so let’s talk about its basic usage first. The usage of
the hash
set unordered_set must include the relevant header files

#include <unordered_set>

definition

unordered_set<type> hashset

usage

hashset.erase(type a)Delete the elements of a in the set

Example:
hashset data is {'a', 'b', 'c'}
hashset.erase('a') result becomes {'b', 'c'}

hashset.insert(type a)Insert an element into the collection

Example:
the result of hashset.insert('t') becomes {'b','c','t'}

hashset.count('b')Count the number of elements 'b' in the collection

Process analysis:

  • The length n of the string s is used as the total number of cycles, and i is the current number of cycles
  • If i!=0, the hash set finds the s[i] value of the i-th bit of the string and deletes it
  • If there is no value equal to the value of the new s[rk+1] to be inserted in the hash set, insert it into the hash set.
  • If equal, do not insert. And calculate the length of the hash set at this time, take the largest length and save it
  • Return to the first step until the end of the loop

#include <iostream>
#include <unordered_set>
using namespace std;

class Solution {
    
    
public:

    int lengthOfLongestSubstring(string s) {
    
    
        // 哈希集合,记录每个字符是否出现过
        unordered_set<char> occ;
        int n = s.size();
        cout<<"s size:"<<n<<endl;
        // 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
        int rk = -1, ans = 0;
        // 枚举左指针的位置,初始值隐性地表示为 -1
        for (int i = 0; i < n; ++i) {
    
    
            if (i != 0) {
    
    
                // 左指针向右移动一格,移除一个字符
                occ.erase(s[i - 1]);
            }

            while (rk + 1 < n && !occ.count(s[rk + 1])) {
    
    
                auto flag = occ.count(s[rk + 1]);
                auto a = rk+1;
                // 不断地移动右指针
                cout<<"insert count: "<<rk+1<<"   insert s: "<<s[rk+1]<<endl;
                occ.insert(s[rk + 1]);
                ++rk;
                cout<<"rk:"<<rk<<endl;
            }
            // 第 i 到 rk 个字符是一个极长的无重复字符子串
            auto flag = occ.count(s[rk + 1]);
            cerr<<"occ count: "<<flag<<" rk:"<<rk<<" i: "<<i<<endl;
            ans = max(ans, rk - i + 1);
            cerr<<"ans:"<<ans<<endl;
        }
        return ans;
    }
};

int main(){
    
    
    Solution S;

    string s = "abdegdgddads";

    S.lengthOfLongestSubstring(s);

    return 0;
}


Guess you like

Origin blog.csdn.net/m0_49302377/article/details/130837165