1 topics
Given 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.
Hide TagsHash Table Pointers String2 thinking began to think is to get all the non-repetition of the line, and later found to be continuous substring. Then think of every time you encounter duplicates, from that point the next one begins to re-add. Later, a bug test input was encountered and time timed out. See other people's ideas, using the topic hint of the pointers. Also, the table's key,value settings are fastidious, key is the letter, and value is the position. The key I started thinking about was a different location. Original link: Https://leetcode.com/discuss/17939/my-accepted-o-n-java-solutionok, see Code 3 code
Public intlengthoflongestsubstring (String s) {Hashtable<Character,Integer> hash=NewHashtable<>(); intLength=s.length (); intMax=0; intAvailablefrom=0; for(inti=0;i<length;i++){ if(Hash.containskey (S.charat (i))) {//int last = The largest index where have the same character (before current index i) intlast=(Integer) hash.get (S.charat (i)); //int available-from = The next index from where latest duplication ends (before current index i) //The biggest, prevent ABBA, traverse to the second A, last will be smaller than AvailableformAvailablefrom=math.max (AvailableFrom, last+1); } //Then the possible substring is located between Available-from and I. Max=math.max (Max, i-availablefrom+1); Hash.put (S.charat (i), i); } returnMax; }
[Leet Code 3] Longest Substring without repeating characters