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;
相关推荐
Tisfy13 小时前
LeetCode 3882.网格图中最小异或路径:动态规划
算法·leetcode·动态规划
D e re k ZH14 小时前
P5025 [SNOI2017] 炸弹
c++·算法
A boy CDEF girl14 小时前
算法题专栏:优选算法
数据结构·算法·leetcode
没落之王14 小时前
三易五行看宋朝的三界确立
大数据·人工智能·算法
坤坤子吖15 小时前
算法学习——单调队列
开发语言·c++·笔记·学习·算法
Js.Tx16 小时前
【具体数学 Concrete Mathematics】2.1.5 一般性的方法
算法·具体数学
秋天的怀念_David18 小时前
算法一千零一夜 · 作品相关:版权与致谢声明
程序人生·算法·学习方法
揽星河..18 小时前
【C语言】数组
c语言·开发语言·算法
careathers18 小时前
【数据结构】基于比较的排序算法 - 选择排序
数据结构·算法·排序算法
听风若依18 小时前
C++模板初阶
c++·算法