0219leetcode brushes 5 python questions

14

Title description:
Write a function to find the longest common prefix in a string array.
If there is no common prefix, an empty string "" is returned.

Example:
Insert picture description here
Answer:

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if not strs:
            return ""
        s1=min(strs)
        s2=max(strs)
        for i,x in enumerate(s1):
            if x!=s2[i]:
                return s2[:i]
        return s1

15

Title description:
Give you an array nums containing n integers, and judge whether there are three elements a, b, c in nums such that a + b + c = 0? Please find all triples whose sum is 0 and does not repeat.
Note: The answer cannot contain repeated triples.

Example:
Insert picture description here
Answer:

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        res =[]
        i = 0
        for i in range(len(nums)):
            if i == 0 or nums[i]>nums[i-1]:
                l = i+1
                r = len(nums)-1
                while l < r:
                    s = nums[i] + nums[l] +nums[r]
                    if s ==0:
                        res.append([nums[i],nums[l],nums[r]])
                        l +=1
                        r -=1
                        while l < r and nums[l] == nums[l-1]:
                            l += 1
                        while r > l and nums[r] == nums[r+1]:
                            r -= 1
                    elif s>0:
                        r -=1
                    else :
                        l +=1
        return res

24

Title description:
Given a linked list, exchange adjacent nodes in it two by one, and return the exchanged linked list.
You can't just simply change the internal value of the node, but need to actually exchange the node.

Example:
Insert picture description here
Answer:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        pre,cur=head,head.next
        pre.next=self.swapPairs(cur.next)
        cur.next=pre
        return cur

25

Topic description:
Give you a linked list, every k nodes are flipped, please return to the flipped linked list.
k is a positive integer, and its value is less than or equal to the length of the linked list.
If the total number of nodes is not an integral multiple of k, please keep the last remaining nodes in the original order.

Example:
Insert picture description here
Answer:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        #递归
        cur=head
        count=0
        while cur and count!=k:
            cur=cur.next
            count+=1
        if count==k:
            cur=self.reverseKGroup(cur,k)
            while count:
                tmp=head.next
                head.next=cur
                cur=head
                head=tmp
                count-=1
            head=cur
        return head

995

Title description;
In the array A containing only 0 and 1, a K bit flip includes selecting a (continuous) sub-array of length K, and at the same time changing each 0 in the sub-array to 1, and changing each 1 to 0.
Returns the minimum number of K bit flips required so that the array has no elements with value 0. If it is not possible, return -1.

Example:
Insert picture description here
Answer:

class Solution:
    def minKBitFlips(self, A: List[int], K: int) -> int:
        i, rev = 0, []
        for j, num in enumerate(A):
            while i < len(rev) and j - rev[i] >= K:
                i += 1
            if (len(rev) - i) & 1 == num:
                if j > len(A) - K:
                    return -1
                else:
                    rev.append(j)
        return len(rev)

Guess you like

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