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;
相关推荐
Frostnova丶6 分钟前
(10)LeetCode 560. 和为K的子数组
算法·leetcode·哈希算法
AI专业测评10 分钟前
2026年AI写作软件底层技术全景解析:长篇AI写网文的工程化实践与AI消痕算法基准测试
人工智能·算法·ai写作
2401_8845632416 分钟前
高性能日志库C++实现
开发语言·c++·算法
葳_人生_蕤16 分钟前
hot100——226.翻转二叉树
算法
handler0121 分钟前
基础算法:BFS
开发语言·数据结构·c++·学习·算法·宽度优先
2401_8795034122 分钟前
C++中的状态模式实战
开发语言·c++·算法
不当菜鸡的程序媛23 分钟前
神经网络——bias 偏置项(bias term) 或者截距项(intercept term)
人工智能·神经网络·算法
Aawy12023 分钟前
自定义字面量实战
开发语言·c++·算法
无尽的罚坐人生25 分钟前
hot 100 200. 岛屿数量
算法·dfs
j_xxx404_29 分钟前
LeetCode模拟算法精解II:外观数列与数青蛙
数据结构·c++·算法·leetcode