Sword refers to Offer 48. The longest substring without repeating characters

1. Sliding window (unordered_set container simulation)

Idea:
Use the unordered_set feature: the elements contained are unique
Use the unordered_set container to simulate the window, and use two indexes to manage the left and right borders of the window

class Solution {
    
    
public:
    int lengthOfLongestSubstring(string s) {
    
    
        unordered_set<char> sset;
        int Max = 0;
        int i = 0, j = 0;
        int len = s.length();
        while(j < len){
    
    
            if(sset.count(s[j])){
    
    		//判断当前遍历字符是否重复
                sset.erase(s[i]);
                i++;
            }
            else{
    
    
                sset.insert(s[j++]);	//无重复的话即插入容器
            }
            Max = max(Max, j - i);
        }
        return Max;
    }
};

**整理不易 你的点赞、关注是对我莫大的鼓励**
![在这里插入图片描述](https://img-blog.csdnimg.cn/644b8f5dde414d65822c99496adc5148.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pivIFRoZSBMaW4g5ZGA,size_15,color_FFFFFF,t_70,g_se,x_16#pic_center)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324423561&siteId=291194637