Pointers with HashSet
Equivalent to control a small window, if the next letter is a new letter, the small window will move to the right one grid, while checking whether it is the maximum length, if the next letter is duplicated, then let the left side of the small window to the right to the first is not the letter of the place.
Note that the small window to the left of the time, the right side of the small window to move to the right, or the next round of inspection, for the same letter will be checked two times, the front is deleted
1 Public intlengthoflongestsubstring (String s) {2 if(s = =NULL|| S.length () = = 0) {3 return0;4 } 5 intWalker = 0;6 intRunner = 0;7 intMaxLen = 0;8Set<character> set =NewHashset<character>();9 while(Runner <s.length ()) {Ten Charc =S.charat (runner); One if(Set.contains (c)) { A while(S.charat Walker)! =c) { - Set.remove (S.charat (Walker)); -walker++; the } -walker++; -runner++; -}Else { + Set.add (c); -runner++; +MaxLen = (MaxLen < Runner-walker)? (Runner-Walker): MaxLen; A } at } - returnMaxLen; -}
Bug Record:
Made a particularly hidden silly mistake, in the 13th, 14 line of the place, I first walker++, then remove, then deleted the wrong letter .......
3. Longest Substring without repeating characters