Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
经典Hashtable + two pointer的题目。时间复杂度O(n), 空间复杂度O(n)
1 public class Solution { 2 public int lengthOfLongestSubstring(String s) { 3 int res = 0, start = 0; 4 Mapmap = new HashMap<>(); 5 for(int i = 0; i < s.length(); i++){ 6 char c = s.charAt(i); 7 if(map.containsKey(c)){ 8 start = Math.max(start, map.get(c)+1); 9 }10 map.put(c, i);11 res = Math.max(res, i-start+1);12 }13 return res;14 }15 }