0218leetcode brushes 5 python questions

566

Title description:
In MATLAB, there is a very useful function reshape, which can reshape a matrix into another new matrix with a different size, but retain its original data.
Given a matrix represented by a two-dimensional array, and two positive integers r and c, representing the number of rows and columns of the desired reconstructed matrix, respectively.
The reconstructed matrix needs to fill all the elements of the original matrix in the same row traversal order.
If the reshape operation with the given parameters is feasible and reasonable, the new reshape matrix is ​​output; otherwise, the original matrix is ​​output.

Example:
Insert picture description here
Answer:

class Solution:
    def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
        if len(nums)*len(nums[0])!=r*c:
            return nums
        l=[]
        for i in nums:
            l+=i
        return [[l.pop(0) for i in range(c)] for j in range(r)]

832

Title description:
Given a binary matrix A, we want to flip the image horizontally first, then invert the image and return the result.
To flip a picture horizontally is to flip each line of the picture, that is, reverse the order. For example, the result of horizontally flipping [1, 1, 0] is [0, 1, 1].
Inverting the picture means that all 0s in the picture are replaced by 1s, and all 1s are replaced by 0s. For example, the result of reversing [0, 1, 1] is [1, 0, 0].

Example:
Insert picture description here
Answer:

class Solution:
    def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
        for i in range(len(A)):
            for j in range((len(A)+1)//2):
                if A[i][j]==A[i][-1-j]:
                    t=1-A[i][j]
                    A[i][j]=A[i][-1-j]=t
        return A

1160

Title description:
Give you a "word list" (string array) words and a "alphabet" (string) chars.
If you can use the "letters" (characters) in chars to spell out a certain "word" (string) in words, then we think you have mastered the word.
Note: Each time you spell (referring to spelling a word in the vocabulary), each letter in chars can only be used once.
Returns the sum of the lengths of all the words you have in the vocabulary words.

Example:
Insert picture description here
Answer:

class Solution:
    def countCharacters(self, words: List[str], chars: str) -> int:
        ans = 0
        for w in words:
            for i in w:
                if w.count(i) <= chars.count(i):
                    flag = 1
                    continue
                else:
                    flag = 0
                    break
            if flag == 1:
                ans+=len(w)
        return ans

1370

Title description:
Give you a string s, please reconstruct the string according to the following algorithm:
select the smallest character from s and connect it to the result string.
Select the smallest character from the remaining characters in s, and the character is larger than the last added character, and it is appended to the result string.
Repeat step 2 until you cannot select characters from s.
Select the largest character from s and connect it to the end of the result string.
Select the largest character from the remaining characters in s, and the character is smaller than the last added character, and it is appended to the result string.
Repeat step 5 until you cannot select characters from s.
Repeat steps 1 to 6 until all characters in s have been selected.
In any step, if there is more than one minimum or maximum character, you can select any one of them and add it to the result string.
Please return the resulting string after reordering the characters in s.

Example:
Insert picture description here
Answer:

class Solution:
    def sortString(self, s: str) -> str:
        c = collections.Counter(s)
        keys, ans, flag = sorted(c.keys()), "", True
        while flag:
            flag = False
            for k in keys:
                if c[k] > 0:
                    ans += k
                    c[k] -= 1
                if c[k] > 0:    
                    flag = True
            keys = keys[::-1]
        return ans  

1576

Title description:
Give you a string s that only contains lowercase English letters and'?' characters. Please convert all the'?' into several lowercase letters so that the final string does not contain any consecutive repeated characters.
Note: You cannot modify non-'?' characters.
The question test case guarantees that there are no consecutive repeated characters except for the'?' character.
Return the final string after all conversions are completed (maybe no conversions are needed). If there are multiple solutions, please return to any one of them. It can be proved that under the given constraints, the answer always exists.

Example:
Insert picture description here
Answer:

class Solution:
    def modifyString(self, s: str) -> str:
        y="abcdefghijklmnopqrstuvwxyz"
        for i in s:
            if i in y:
                y=y.replace(i,'')
        h=0
        s_list=list(s)
        y_list=list(y)
        for i in range(len(s_list)):
            if s_list[i]=="?":
                s_list[i]=y_list[h]
                h+=1
            if h+1==len(y_list):
                h=0
        return ''.join(s_list)

Guess you like

Origin blog.csdn.net/yeqing1997/article/details/113822293