0
点赞
收藏
分享

微信扫一扫

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

贵州谢高低 2022-03-18 阅读 159
c++
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_set>
using namespace std;

/*
* 思路:滑动窗口问题,向右滑动时有重复字符时,收缩左边窗口边界
*/

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_set<char>str;
        int left = 0, max_len = 0;
        for (int i = 0; i < s.length(); i++)
        {
            while (str.find(s[i]) != str.end())
            {
                str.erase(s[left]);
                left++;
            }
            max_len = max(max_len, i - left + 1);
            str.insert(s[i]);
        }
        return max_len;
    }
};

int main()
{
    string s;
    cin >> s;
    Solution A;
    cout << A.lengthOfLongestSubstring(s) << endl;
}
举报

相关推荐

0 条评论