Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
代碼如下:
1 public class Solution { 2 public int lengthOfLongestSubstring(String s) { 3 int max=0; 4 char[] ss=s.toCharArray(); 5 6 for(int i=0;i<ss.length;i++) 7 { 8 int count=0; 9 Map<Character,Character> map=new HashMap<>(); 10 for(int j=i;j<ss.length;j++) 11 { 12 if(!map.containsKey(ss[j])) 13 { 14 map.put(ss[j],ss[j]); 15 count++; 16 } 17 else break; 18 } 19 if(count>max) 20 max=count; 21 } 22 return max; 23 } 24 }