Skip to main content

🟡 剑指 Offer II 016. 不含重复字符的最长子字符串

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解1#

class Solution {    public int lengthOfLongestSubstring(String s) {        Set<Character> visited = new HashSet<>();
        int lo=0, hi=0, res=0;        char cur;
        while (hi < s.length()) {            cur = s.charAt(hi);            if (visited.contains(cur)) {                while (s.charAt(lo) != cur && lo < hi) {                    visited.remove(s.charAt(lo));                    lo += 1;                }                lo += 1;            } else {                res = Math.max(res, hi-lo+1);            }            visited.add(cur);            hi += 1;        }
        return res;    }}