This week in front of the brush is quite happy, behind a lot of other things and problems to go to busy other, the result and only completed the minimum goal.
Lonest Substring without repeating characters:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb" , the answer "abc" is, which the length is 3.
Given "bbbbb" , the answer "b" is, with the length of 1.
Given "pwwkew" , the answer "wke" is, with the length of 3. Note that the answer must was a substring, is "pwke" a subsequence and not a substring.
First of all, this is a medium-difficulty problem, and the first thing I think about is doing a pointer to the head, and then dialing the pointer forward when it encounters the same character as the head pointer. At the time I tried to simplify the problem to a different character length between the two identical characters. Later I thought about it and found that the general direction is not a big mistake, but the boundary conditions are many, including the continuous encounter two of the same characters need to do the processing, there are no two of the same characters need to do the processing and so on. Try your own ideas, write the code below the AC code:
classsolution (object):deflengthoflongestsubstring (self, s):""": Type S:str:rtype:int"""Longest= Head =0 visited= [False for_inchXrange (256)] forIndex, CharinchEnumerate (s):ifVisited[ord (char)]: whileS[head]! =Char:visited[ord (S[head])=False Head+ = 1Head+ = 1Else: Visited[ord (char)]=True Longest= Max (Longest, Index-head + 1) returnLongest
The following is again to explain the correct problem-solving ideas.
1. First, it is difficult to understand is the initialization of an ASCII table 256 characters, this thing to ensure that every possible character can be recorded, once it appears to set it to true. Of course, you can also use the dictionary to achieve this function, here will not repeat.
2. Then turn on the loop and then iterate through the string.
3. See if the index of the ASCII table that corresponds to the current char is present.
4. If there is a occurrence, then turn on the loop and determine if the character of the head position equals the character of the loop. Here are some reasons to make this judgment. Because there may be two consecutive identical characters when judging a string, such as ' BBA ', when the ' BBA ' enters the loop, the head place and the char of the loop will be equal, so directly let head+1 this time head is 1 index is 1 Then do the longest calculation to get the correct 1-bit answer.
5. If there is a second occurrence of the character, and the head corresponding position of the character and char is not equal what is the case. For example ' BAPOIUBCBD ' when encountering the second B will enter the loop, this time the head pointer to B and then head starts to point to a. It's normal to be here, but it's immediately followed by a third B. This time we go into the loop, this time we're going to do is to move the pointer to the second B position, and clear the middle of all occurrences of the characters, why? began to introduce the question when it has been said that the reason for self-study analysis, is actually looking for two of the same character between the maximum length (except the boundary conditions abstract this problem). So when the third B appears, you should move the pointer to the second B. And the middle of the missing characters are reset to no access, so that the second time to calculate the length of the longest, used to compare with the longest longest occurred.
There is nothing more to say about this problem.
Median of Sorted Arrays:
There is sorted arrays nums1 and nums2 of size m and N respectively.
Find The median of the sorted arrays. The overall run time complexity should be O (log (m+n)).
Example 1= [1, 3= [2 is 2.0
Example 2= [1, 2= [3, 4 is (2 + 3)/2 = 2.5
This is a question to ask for the median, I got the question when I looked at the difficulty of hard tough a bit strange, the normal solution is not particularly complex, but the problem requires a time complexity, perhaps this is the key to difficult it. I thought about it when I got the problem, because two arrays were ordered, but I didn't think much of it. In fact, if Python comes with a sort function sorted, you can merge two arrays and sort them. After the conversion to a median problem, distinguish between parity, and finally AC. The order of the visible sorted functions is very efficient.
In addition, on the Internet to see a method, is to transform the problem into a kth. I actually converted to a half-day, how does this problem translate into a kth question? First we can think of M-N as an array, even if it's a random order, of course, if they're not out of order, you can use the method mentioned above directly, using m+n even/2-1 cardinality +1/2.
You can use the partition method to find the kth, then you can ensure that the selected number before or after the order must be ordered, so that after determining the length of the entire array, you can know that the median should be the number of digits. From this number of numbers into a problem of seeking kth.
classSolution:deffindkthsortedarrays (self, A, B, K):ifLen (A) <Len (b): A, B=B, AifLen (B) = =0:returnA[k-1] ifK = = 1: returnmin (a[0], b[0]) PB= Min (k/2, Len (B)) PA= K-PBifA[pa-1] > B[pb-1]: returnSelf.findkthsortedarrays (A, B[PB:], K-pb)elifA[pa-1] < B[pb-1]: returnSelf.findkthsortedarrays (A[PA:], B, K-PA)Else: returnA[pa-1] #@return A float deffindmediansortedarrays (self, A, B):if(Len (A) + len (B))% 2 = = 1: returnSelf.findkthsortedarrays (A, B, Len (a) + len (B))/2 + 1) Else: return(Self.findkthsortedarrays (A, B, Len (a) + len (B))/2) +Self.findkthsortedarrays (A, B, Len (a)+ len (B))/2 + 1)/2.0
Above.
Weekly Brush Title Phase II Summary (longest Substring without repeating characters and Median of two Sorted Arrays)