Oh, my God. This question is killing me. I didn't even understand the question at the beginning, that is, I didn't know what substring meant. It took a long time to see it.
Glorious history... Food is dead
1. Question
Given a string, find the length of the longest substring without repeating characters.
Example 1: input: "abcabcbb" output: 3 explanation: the answer is
"abc"
, With the length of 3.
Example 2: input: "bbbbb" output: 1 explanation: the answer is
"b"
, With the length of 1. Example 3: input: "pwwkew" output: 3 explanation: the answer is
"wke"
, With the length of 3. Note that the answer must be a substring,
"pwke"
Is
SubsequenceAnd not a substring.
It means to give a string and find the longest non-duplicate substring. For example, "abcabcbb": "ABC", "ABC", "B", and "pwwkew": "PW", "wke ", "Kew ". Of course, the longest length is 3. Instead of "pwke ". Because he is looking for a substring, it is not a non-repeating sequence. "Dvdf": its non-duplicate substring is "DV", and "VDF" is white, that is, the traversal of one character and one character, and the Child string without repeated characters is found. It took a lot of time to understand this. 2. Python Solution
Class solution: def lengthoflongestsubstring (self, S): "": type S: Str: Rtype: INT "r =" "# store a non-duplicate substring maxnum = 0 # Maximum length of the non-duplicate substring for I in S: If I not in R: # If it is not in the substrings, it indicates that there are no duplicates. If it is directly inserted into the r = R + I else: # if it is in the substrings, it indicates that it is repeated, cannot be directly inserted into if Len (r)> maxnum: maxnum = Len (r) # Calculate the length of the previous substring r = R [R. index (I) + 1:] + I # retained after the same character. For example, "dvdf ". When the first substring is "DV" and then traversed to D, we need to keep the V behind the first substring D and then form a "VD" substring so that there is no repeated substring. If V is not retained, it is not a complete non-duplicate substring. If Len (r)> maxnum: maxnum = Len (r) return maxnums = "dvdf" A = solution () print (. lengthoflongestsubstring (s ))
The explanation code is clearly written. Running Performance is good
I have spent almost two hours writing the C language. According to this idea, there is no problem at all.
Leetcode -- problem3: longest substring without repeating characters