美文网首页
3. 无重复字符的最长子串

3. 无重复字符的最长子串

作者: Andysys | 来源:发表于2019-12-29 00:21 被阅读0次
    // 滑动窗口
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        int n = s.length(), ans = 0;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0, j = 0; j < n; j++) {
            if (map.containsKey(s.charAt(j))) {
                i = Math.max(i, map.get(s.charAt(j)) + 1);
            }
            map.put(s.charAt(j), j);
            ans = Math.max(ans, j - i + 1);
        }
        return ans;
    }

相关文章

网友评论

      本文标题:3. 无重复字符的最长子串

      本文链接:https://www.haomeiwen.com/subject/fzveoctx.html