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;
相关推荐
Mem0rin8 小时前
前缀和:一维与二维
数据结构·算法
小趴蔡ha8 小时前
12 K-Means 聚类入门:没有标签如何发现数据分组
算法·kmeans·聚类
-dzk-8 小时前
【二叉树】LC 101.对称二叉树
算法·二叉树
白狐_7988 小时前
408数据结构第6章:图——核心考点、算法对比与AOE网真题
数据结构·算法
变量未定义~8 小时前
DFS之剪枝与优化-小猫爬山、 数独、 木棒
算法
Navigator_Z8 小时前
LeetCode //C - 1187. Make Array Strictly Increasing
c语言·算法·leetcode
龍德明宇9 小时前
如何理解大语言模型的负主体性-龍德明宇
人工智能·算法·大语言模型llm·负主体性·ai存在论
白狐_7989 小时前
408数据结构第6章:迪杰斯特拉(Dijkstra)算法——真题精讲
数据结构·算法
皓月斯语10 小时前
P2858 [USACO06FEB] Treats for the Cows G/S
数据结构·c++·算法·动态规划
旖旎夜光10 小时前
LeetCode 397:整数替换(贪心问题) —— 题解
数据结构·c++·算法·leetcode·贪心算法