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;
相关推荐
m0_547486668 小时前
《智能计算》全套课件PDF(武汉工程大学)
算法·贝叶斯算法·智能计算
Shaoshing8 小时前
LeetCode100 32.最长有效括号
java·数据结构·leetcode
weixin_307779138 小时前
Jupyter SQL 表血缘分析并导出 Excel
开发语言·python·sql·算法·excel
Lazyafei8 小时前
560. 和为 K 的子数组
数据结构·算法
小白的高手之路8 小时前
【LeetCode 热题 100】240. 搜索二维矩阵 II|矩阵、二分查找、Z 字形查找(Python)
python·算法·leetcode·矩阵·力扣
geovindu8 小时前
python:DBSCAN
开发语言·后端·python·算法
劲夫学编程9 小时前
leetcode:最长连续序列
leetcode·最长连续序列
Tim_109 小时前
【LeetCode】 525. 连续数组
算法
小蒋学算法9 小时前
算法-将数据流变为多个不相交区间-TreeMap
算法
Mortalbreeze9 小时前
图论算法篇:A*、Floyd、Bellman-Ford、SPFA
数据结构·c++·算法