0220leetcode brushes 5 python questions

2

Title description:
Give you two non-empty linked lists, representing two non-negative integers. Each digit of them is stored in reverse order, and each node can only store one digit.
Please add two numbers together and return a linked list representing the sum in the same form.
You can assume that except for the number 0, neither of these numbers will start with 0.

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 addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        if not l1:
            return l2
        if not l2:
            return l1
        val=l1.val+l2.val
        ansnode=ListNode(val%10)
        ansnode.next=self.addTwoNumbers(l1.next,l2.next)
        
        if val>=10:
            ansnode.next=self.addTwoNumbers(ListNode(1),ansnode.next)
        return ansnode

18

Title description:
Given an array nums containing n integers and a target value target, judge whether there are four elements a, b, c, and d in nums so that the value of a + b + c + d is equal to target? Find all four-tuples that meet the conditions and do not repeat.
Note: The answer cannot contain repeated quadruples.

Example:
Insert picture description here
Answer:

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        nums=sorted(nums)
        n=len(nums)
        if len(nums)<4:
            return []
        res=[]
        for i in range(n-3):
            if i>0 and nums[i]==nums[i-1]:
                continue
            if nums[i]+3*nums[i+1]>target:
                break
            if nums[i]+3*nums[-1]<target:
                continue
            for j in range(i+1,n-2):
                if j>i+1 and nums[j]==nums[j-1]:
                    continue
                if nums[i]+nums[j]+2*nums[j+1] > target:
                    break
                if nums[i]+nums[j]+2*nums[-1] < target:
                    continue
                low, high = j + 1, len(nums) - 1
                while low < high:
                    if nums[i] + nums[j] + nums[low] + nums[high] == target:
                        res.append([nums[i], nums[j], nums[low], nums[high]])
                        low, high = low + 1, high - 1
                        while low < high and nums[low] == nums[low - 1]:
                            low += 1
                        while low < high and nums[high] == nums[high + 1]:
                            high -= 1
                    if nums[i] + nums[j] + nums[low] + nums[high] > target:
                        high -= 1
                    if nums[i] + nums[j] + nums[low] + nums[high] < target:
                        low += 1
        return res

19

Title description:
Give you a linked list, delete the nth node from the bottom of the linked list, and return the head node of the linked list.

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 removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        a,b=head,head

        for i in range(n):
            if a.next:
                a=a.next
            else:
                return head.next
        
        while a.next:
            a=a.next
            b=b.next
        b.next=b.next.next
        return head

23

Title description:
Give you an array of linked lists, each of which has been arranged in ascending order.
Please merge all linked lists into one ascending linked list and return the merged linked list.

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 mergeKLists(self, lists: List[ListNode]) -> ListNode:
        import heapq
        dummy = ListNode(0)
        p = dummy
        head = []
        for i in range(len(lists)):
            if lists[i] :
                heapq.heappush(head, (lists[i].val, i))
                lists[i] = lists[i].next
        while head:
            val, idx = heapq.heappop(head)
            p.next = ListNode(val)
            p = p.next
            if lists[idx]:
                heapq.heappush(head, (lists[idx].val, idx))
                lists[idx] = lists[idx].next
        return dummy.next

697

Title description:
Given a non-empty integer array nums containing only non-negative numbers, the degree of the array is defined as the maximum value of any element in the exponent group.
Your task is to find the shortest contiguous sub-array in nums with the same degree as nums, and return its length.

Example:
Insert picture description here
Answer:

class Solution:
    def findShortestSubArray(self, nums: List[int]) -> int:
        a={
    
    }
        for i in range(len(nums)):
            if nums[i] not in a:
                a[nums[i]]=[i]
            else:
                a[nums[i]].append(i)
        m=0
        for i in a:
            m=max(m,len(a[i]))
        r=len(nums)
        for i in a:
            if len(a[i])==m:
                r=min(r,a[i][-1]-a[i][0]+1)
        return r

Guess you like

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