Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

3. Longest Substring Without Repeating Characters #15

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

使用例子题目中的例子带入尝试 public class Solution {
public int lengthOfLongestSubstring(String s) { if(s == "" || s.length() == 0) { return 0; } int max = Integer.MIN_VALUE;
int start = 0;
HashMap<Character,Integer> map = new HashMap<Character, Integer>();

    for(int i=0; i<s.length(); i++) {  
        char c = s.charAt(i);  
        if( map.containsKey(c)){  
            for(int j=start;j<i;j++) {  
                if(s.charAt(j)==c) break;  
                map.remove(s.charAt(j));  
            }  
            start = map.get(c)+1;  
            map.put(c,i);  
        } else {  
            map.put(c,i);  
            if( i-start+1 > max ) max = i-start+1;  
        }  
    }  
    return max;  
}  

}

Shawngbk commented 7 years ago