leetcode-4.22[215. 數組中的第K個最大元素、206. 反轉鏈表、198. 打家劫舍](python解法)

題目1

在這裏插入圖片描述

題目2

class Solution:
    def findKthLargest(self, nums: List[int], k: int) -> int:
        # 遞歸實現快速排序
        def quickSort(nums):
            if len(nums) <= 1:
                return nums
            left = []
            right = []
            for i in nums[1:]:
                if i <= nums[0]:
                    left.append(i)
                else:
                    right.append(i)
            return quickSort(right) + [nums[0]] + quickSort(left)

        return quickSort(nums)[k-1]

題目2

在這裏插入圖片描述

題解2

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head is None:  # 鏈表爲空
            return None
        if head.next is None: # 鏈表只有一個頭節點
            return head
        Pre = None
        Next = head
        while Next:
            Next = Next.next
            head.next = Pre
            Pre = head
            head = Next
        return Pre

題目3

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章