TopicGiven A string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "ABCABCBB" are "abc", which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.
This problem seems simple, but it is very troublesome to use the traversal method. The following method of HashMap treatment, is seen in the discuss, very ingenious.
Code
public class Solution {public int lengthoflongestsubstring (String s) { if (s.length () ==0) return 0; Hashmap<character,integer> map=new hashmap<character,integer> (); int max=0; for (int i=0,j=0;i<s.length (); i++) { if (Map.containskey (S.charat (i))) { j = Math.max (J,map.get (S.charat ( i)) +1); } Map.put (S.charat (i), i); max = Math.max (max,i-j+1); } return max; } }
code Download: Https://github.com/jimenbian/GarvinLeetCode
/********************************
* This article from the blog "Bo Li Garvin"
* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod
******************************************/
"Leetcode from zero single row" No 3 longest Substring without repeating characters