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;
相关推荐
故事和你915 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
脱氧核糖核酸__5 小时前
LeetCode热题100——53.最大子数组和(题解+答案+要点)
数据结构·c++·算法·leetcode
脱氧核糖核酸__6 小时前
LeetCode 热题100——42.接雨水(题目+题解+答案)
数据结构·c++·算法·leetcode
王老师青少年编程7 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:数列分段 Section I
c++·算法·编程·贪心·csp·信奥赛·线性扫描贪心
王老师青少年编程7 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:分糖果
c++·算法·贪心算法·csp·信奥赛·线性扫描贪心·分糖果
_日拱一卒7 小时前
LeetCode:2两数相加
算法·leetcode·职场和发展
py有趣7 小时前
力扣热门100题之零钱兑换
算法·leetcode
董董灿是个攻城狮7 小时前
Opus 4.7 来了,我并不建议你升级
算法
无敌昊哥战神8 小时前
【保姆级题解】力扣17. 电话号码的字母组合 (回溯算法经典入门) | Python/C/C++多语言详解
c语言·c++·python·算法·leetcode
脱氧核糖核酸__8 小时前
LeetCode热题100——238.除了自身以外数组的乘积(题目+题解+答案)
数据结构·c++·算法·leetcode