首页 > 其他分享 >3.longest-substring-without-repeating-characters 无重复字符的最长子串

3.longest-substring-without-repeating-characters 无重复字符的最长子串

时间:2022-12-06 19:44:44浏览次数:67  
标签:子串 字符 arr int res substring ++ without longest

问题描述

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

解题思路

arr[96]记录每个字符出现的次数,如果字符次数大于1,说明已经包含了重复字符,则要更新l,否则递增r,并记录最大的子串长度。

代码

class Solution {
  public:
    int lengthOfLongestSubstring(string s) {
        int arr[96] = {0};
        int res = 0;
        int l = 0;
        for (int r = 0; r < s.size(); r++) {
            arr[s[r] - ' ']++;
            if (arr[s[r] - ' '] > 1) {
                while (l < r && arr[s[r] - ' '] > 1)
                    arr[s[l++] - ' ']--;
            } else {
                res = max(r - l + 1, res);
            }
        }
        return res;
    }
};

标签:子串,字符,arr,int,res,substring,++,without,longest
From: https://www.cnblogs.com/zwyyy456/p/16960302.html

相关文章