请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
示例 1:
输入: "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
滑动窗口解题思路:用2个指针控制一个不重复的字符区间
/** * @param {string} s * @return {number} */ var lengthOfLongestSubstring = function(s) { let len = s.length; if(len === 0) return 0; let ans = 1; let l = 0; let r = 1; while(r < len){ let i = r-1; while(i >= l){ if(s[r] === s[i]){ ans = r-i>ans?r-i:ans; l = i+1; break; } if(i === l){ ans = r-l+1>ans?r-l+1:ans; } i--; } r++; } return ans; };