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;
        
    }

  

posted @ 2019-10-22 09:52  抽象Java  阅读(123)  评论(0编辑  收藏  举报