美文网首页
3. 无重复字符的最长子串(2019-01-12)

3. 无重复字符的最长子串(2019-01-12)

作者: Rondo9 | 来源:发表于2019-01-12 18:06 被阅读0次
无重复字符的最长子串

class Solution {

    private Map<Character, Integer> map = new HashMap<>();

    public int lengthOfLongestSubstring(String s) {

        int i = 0;

        int max = 0;

        char temp = '\0';

        int start = -1;

        for (i = 0; i < s.length(); i ++) {

            temp = s.charAt(i);

            if (map.get(temp) != null) {

                start = Math.max(map.get(temp) , start);

            }

            max = Math.max(i - start, max);

            map.put(temp, i);

        }

        return max;

    }

}

相关文章

网友评论

      本文标题:3. 无重复字符的最长子串(2019-01-12)

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