02. LeetCode Medium

3y 2021-09-02 11:29:03
Categories: Tags:

LeetCode 3

问题:给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

/**
 * 无重复字符的最长子串
 * (滑动指针)
 * 1. 使用字符数组 记录已遍历过的值
 * 2. 只要 r 未越界且 字符数组不存在该值,移动r 且 添加至 字符数组
 * 3. 否则 移动l 且 删除 字符数组
 * 4. result 获取每次移动后的最大值
 * 5. 当字符遍历完成,返回result
 */
public class LeetCode3 {

    public int lengthOfLongestSubstring(String s) {

        int result = 0;
        int l = 0;
        int r = -1;

        char[] memo = new char[666];

        while (l < s.length()) {
            if (r + 1 < s.length() && memo[s.charAt(r + 1)] == 0) {
                memo[s.charAt(++r)]++;
            } else {
                memo[s.charAt(l++)]--;
            }
            result = Math.max(result, r - l + 1);
        }
        return result;
    }

}