I. Preface
recently learning a little intermittent, some of the knowledge points are not complete, or completely do not understand, not good to take the stage face, or first in the draft box lying. Occasionally when browsing Daniel blog http://coolshell.cn , found that Daniel's spare time is also doing programming training http://coolshell.cn/articles/12052. HTML, as a test ape who wants to be an inspirational farmer, should use spare time to train his coding ability and master the foundation. So let's get started,https://oj.leetcode.com/problems/ .
two. Text1. Title: Longest Substring without repeating characters
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.
2. Ideas:
Similar to the simple insertion sorting algorithm, assuming that the first 0–> (i-1) characters are not duplicated, take out the I character in turn from backward to forward, if the occurrence and the character repeated by the I character (its position is J), then recorded (i-1) 0 as the length of the current eldest son string, recorded (j+1) as the next The end of the second traversal, and then takes the first (i+1) character to repeat the above traversal, and if a repeating character is present, the length of the current substring is recorded and compared to the length of the temporary oldest string, updating the oldest string length to a larger value. In turn ... Until the last character of the string is taken, the length of the substring with the last string without repeating characters is recorded, and the previous oldest string length is compared to return a larger value.
3. Problem solving:
/*** The algorithm here is similar to a simple insert sort: * takes the first character in a string, compares it to a character not previously duplicated, and if the same character is found, calculates the number of characters between the first (i-1) character and the same character * For example: 0 1 2 3 4 5 6 7 8 9 Ten * s= "q o p u B J g u x H x"; * For the first traversal, find the position where the repeating character appears 7->u, calculate the length of the previous occurrence of the repeating string is 7, * and record the position of the next character (4->b) of the previous repeated character found, as the end of the next traversal * continue to take the 8th character X, followed by the preceding word transmitting More, until the 4th character B, * continue to take the 9th character H to traverse to the 4th character B * Continue to take the 10th character X to traverse to the 8th characters, repeat, then record 8th characters to 4th characters length is 5, * compare substring length, record longer length value */ Public Static intlengthoflongestsubstring (String s) {intStrlength =s.length (); intMaxLength =-1; intStart = 0; for(inti = 1; i < strlength; i++) { Chartemp =S.charat (i); for(intj = i; J > start; j--) { if(S.charat (j-1) = =temp) {MaxLength= (MaxLength > (I-start))? MaxLength: (istart); Start=J; } } } //if the last traversal has no duplicate characters, the length of the last traversal is compared to the length of the previous oldest stringMaxLength = (MaxLength > (Strlength-start))? MaxLength: (strlength-start); returnmaxLength; }
Test Case One:
Public Static voidMain (string[] args) {//input:s String s= "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\" #$%& ' () *+,-./:;<=>[email& Nbsp;protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[Email protected] [\\]^_`{|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\ "#$%&" () *+,-./:;<=>[email protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ ABCDEFGHIJKLMNOPQRSTUVWXYZABCDefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!\ "#$%&" () *+,-./:;<=>[email Protected][\\]^_ ' {|} ~ ABCDEFGHIJKLMNOPQRSTUVWXYZABCD "; intLength =lengthoflongestsubstring (s); //output:95System.out.println (length); }
Test Case Two:
Public Static void Main (string[] args) { //input:s String s= "Wlrbbmqbhcdarzowkkyhid"; int length = lengthoflongestsubstring (s); // Output:12 System.out.println (length); }
Test Case Three:
Public Static void Main (string[] args) { //input:s String s= " HNWNKUEWHSQMGBBUQCLJJIVSWMDKQTBXIXMVTRRBLJPTNSNFWZQFJMAFADRRWSOFSBCNUVQHFFBSAQXWPQCAC "; int length = lengthoflongestsubstring (s); // Output:12 System.out.println (length); }
Interested friends also go to Leetcode to try it,
https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
The length of the longest string of non-repeating character strings "Java Edition"