美文网首页
Longest Substring Without Repea

Longest Substring Without Repea

作者: Wenyue_offer | 来源:发表于2017-09-12 15:06 被阅读0次

链接

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        if (n==0) return 0;
        
        HashMap<Character,Integer> map = new HashMap<Character,Integer>();
        
        int ans = 0;
        
        for(int i= 0,j=0;i<n;i++){
            if(map.containsKey(s.charAt(i))){
                j = Math.max(j,map.get(s.charAt(i))+1);
            }
            map.put(s.charAt(i),i);
            ans = Math.max(ans,i-j+1);
        }
        return ans;
    }
}

相关文章

网友评论

      本文标题: Longest Substring Without Repea

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