LeetCode3-Longest_Substring_Without_Repeating_Characters

参考思路

https://github.com/azl397985856/leetcode/blob/master/problems/3.longestSubstringWithoutRepeatingCharacters.md

public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> repeatChar = new HashMap<>();
        int left = 0;
        char c;
        int maxLength = 0;
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (!repeatChar.containsKey(c) || (i < left)) {
                repeatChar.put(c, i);
                continue;
            }
            maxLength = Math.max(maxLength,i-left);
            left = Math.max(left,repeatChar.get(c)+1);//有时候重复的在最
            repeatChar.put(c,i);
        }
        maxLength = Math.max(maxLength,s.length() - left);
        return maxLength;
        
    }

  

原文地址:https://www.cnblogs.com/Jomini/p/11718072.html