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;
相关推荐
pen-ai几秒前
【Yolo系列】Yolov3 目标检测算法原理详解
算法·yolo·目标检测
田里的水稻几秒前
EP_基于UWB和单线激光雷达的托盘转送
人工智能·算法·数学建模·机器人·自动驾驶
List<String> error_P2 分钟前
DFS(深度优先搜索)
数据结构·算法·dfs
今儿敲了吗6 分钟前
27| 魔法封印
数据结构·c++·笔记·学习·算法
honortech11 分钟前
算法题中的 mid 中点问题
算法
今儿敲了吗12 分钟前
30| 木材加工
数据结构·c++·笔记·学习·算法
WW_千谷山4_sch18 分钟前
MYOJ_7789:(洛谷P3388)【模板】割点(割顶)(tarjan算法)
c++·算法·深度优先·图论
WZ1881046386918 分钟前
LeetCode第131题
算法·leetcode
锅包一切18 分钟前
PART7 队列
c++·学习·算法·leetcode·力扣·刷题·队列
Maỿbe24 分钟前
动态规划之子数组问题
算法·动态规划