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;
相关推荐
文丑颜不良啊10 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z10 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH11 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶12 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊12 小时前
LeetCode2161~2180题目解析
java·leetcode
雪落漂泊12 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
文丑颜不良啊14 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发14 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
严肃小白兔15 小时前
MATLAB 数值计算中的精度与误差问题:浮点运算、误差传播与数值稳定性
人工智能·算法·matlab