0220leetcode刷题5道python

2

题目描述:
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:
在这里插入图片描述
解答:

# 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

题目描述:
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:答案中不可以包含重复的四元组。

示例:
在这里插入图片描述
解答:

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

题目描述:
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

示例:
在这里插入图片描述
解答:

# 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

题目描述:
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。

示例:
在这里插入图片描述
解答:

# 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

题目描述:
给定一个非空且只包含非负数的整数数组 nums,数组的度的定义是指数组里任一元素出现频数的最大值。
你的任务是在 nums 中找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。

示例:
在这里插入图片描述
解答:

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

猜你喜欢

转载自blog.csdn.net/yeqing1997/article/details/113856815