LeetCode【3】无重复的最长字串

题目:

思路:

双指针,窗口内字符放入HashSet中。

代码:

java 复制代码
public int lengthOfLongestSubstring(String s) {
        int start = 0, end = 0;
        int max = 0;
        Set<Character> set = new HashSet<>();

        while (start < s.length() && end < s.length() && start <= end) {
            if (set.contains(s.charAt(end))) {
                set.remove(s.charAt(start));
                start ++;
            } else {
                set.add(s.charAt(end));
                max = Math.max(max, end - start + 1);
                end ++;
            }
        }

        return max;
相关推荐
文丑颜不良啊13 小时前
LeetCode2481~2500题目解析
java·leetcode
武子康17 小时前
驾驶轨迹换个坐标,就能给移动机器人用吗?
人工智能·算法·机器学习
文丑颜不良啊20 小时前
LeetCode2461~2480题目解析
java·leetcode
文丑颜不良啊21 小时前
LeetCode2441~2460题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2521~2540题目解析
java·leetcode
lisw051 天前
Dayhoff的PAM替换矩阵理论评介!
线性代数·算法·矩阵
一起学安全技术1 天前
拆大模型 03|注意力的一句话版本:不用论文讲清 Attention
深度学习·算法·语言模型·transformer
Delightrs1 天前
Rust LeetCode238 算法题解
开发语言·算法·rust
自信150413057591 天前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
文丑颜不良啊1 天前
LeetCode2501~2520题目解析
java·leetcode