LeetCode_C++_3. The longest substring without repeating characters

insert image description here
Ideas:
(1) Violent traversal

class Solution {
    
    
public:
    int lengthOfLongestSubstring(string s) 
    {
    
       
        string result;
        int max=0;
        int remax = 0;
        bool flag = false;
        int len = s.size();
        if (s[0] == ' '||s.size()==1)
        {
    
    
            remax = 1;
            return remax;
        }
        else
        {
    
     
        for(int k =0;k<len;k++)
        {
    
    
            flag = false;
            result.clear();
            if(remax<max)
            remax = max;
            max = 0;
            for (int i = k;i < len;i++)
            {
    
    
                max++;
                if (i == k)
                {
    
    
                    result.push_back(s[i]);
                }

                else
                {
    
    
                    for (int j = 0;j < result.size();j++)
                    {
    
    
                        if (s[i] == result[j])
                        {
    
    
                            flag = true;
                            continue;
                        }
                    }
                    if (flag == true)
                    {
    
    
                        max = max - 1;
                        break;
                    }
                    else
                        result.push_back(s[i]);
                }
            }
        }

        }
        return remax;
    }
    
};

insert image description here

(2) Sliding window

class Solution {
    
    
public:
    int lengthOfLongestSubstring(string s) {
    
    

            int res = 0;
            int len = s.size();
            int freq[256] = {
    
     0 };
            int l = 0, r = -1;
            while (l < len) {
    
    
                if (r + 1 < len && freq[s[r + 1]] == 0) {
    
    
                    freq[s[++r]]++;
                }
                else {
    
    
                    freq[s[l++]]--;
                }
                res = max(res, r - l + 1);
            }
            return res;
       
    }
};

insert image description here
    In the code of the sliding window, if we encounter a string of pwwkew because there are two consecutive Ws, what we want is to let the left pointer directly come to the second W. According to the code, we can first let the left pointer directly cross the right pointer. to the right, and move the right pointer over.
    Through the code, the value of W pointed by the left pointer is changed to -1, so that the value of the object pointed by the right pointer is not 0, and the movement continues. It is difficult to understand.

(3)
    The code of the sliding window at the core of the hash table + sliding window remains unchanged, but we need to think about how to point the left pointer to the next element of the repeated element (this is really important, otherwise it will cause many problems) . Using a hash table can achieve the goal by continuously eliminating the elements on the left.

class Solution {
    
    
public:
    int lengthOfLongestSubstring(string s) {
    
    
        // 哈希集合,记录每个字符是否出现过
        unordered_set<char> occ;
        int n = s.size();
        // 右指针,初始值为 -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])) {
    
    
                // 不断地移动右指针
                occ.insert(s[rk + 1]);
                ++rk;
            }
            // 第 i 到 rk 个字符是一个极长的无重复字符子串
            ans = max(ans, rk - i + 1);
        }
        return ans;
    }
};

Guess you like

Origin blog.csdn.net/daweq/article/details/129824575