高频题目总结

数组中的第 k 大的数字

class Solution:
    def findKthLargest(self, nums: List[int], k: int) -> int:

        n = len(nums)
        def partitions(arr, l, r):
            base = arr[l]
            while l < r:
                while l < r and arr[r] > base:
                    r -= 1
                arr[l] = arr[r]
                while l < r and arr[l] <= base:
                    l += 1
                arr[r] = arr[l]
            arr[l] = base
            return l

        def qulick_sort(nums, l,r):
            mid = partitions(nums, l, r)
            if mid == n - k:
                return nums[mid]
            elif mid < (n - k):
                return qulick_sort(nums, mid + 1, r)
            else:
                return qulick_sort(nums, l, mid - 1)

        return qulick_sort(nums, 0, n - 1)

易错总结

  • line6: base = arr[l],别写成了arr[0]
  • line10/13:arr[l] = arr[r]/arr[r] = arr[l] 别丢了
  • line8、line11:base的符号互斥
  • line19: n - k
  • line20:return nums[mid] ,别写成了return mid

33. 搜索旋转排序数组【旋转数组找k大】

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        n = len(nums)
        l, r = 0, n - 1
        while l <= r:
            mid = (l + r) // 2
            base = nums[mid]
            if target == base:
                return mid
            elif nums[0] <= base:
                if nums[0] <= target < base:
                    r = mid - 1
                else:
                    l = mid + 1
            else:
                if base < target <= nums[-1]:
                    l = mid + 1
                else:
                    r = mid - 1
        return -1

易错总结

  • elif nums[0] <= base:少了等号
  • if nums[0] <= target < base和if base < target <= nums[-1],base没有等号约束,因为当base有等号约束的时候,就return了。

347. 前 K 个高频元素

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        import heapq
        dic = {}
        for num in nums:
            if num not in dic:
                dic[num] = 1
            else:
                dic[num] += 1
        heap = []
        for key, val in dic.items():
            if len(heap) >= k:
                if val > heap[0][0]:
                    heapq.heappop(heap)
                    heapq.heappush(heap, (val, key))
            else:
                heapq.heappush(heap, (val, key))   
        return [item[1] for item in heap]

易错总结

  • heapq.heappush(heap, (val, key)),如果你把元组推入heapq,它会以第一个参数作为比较因子,所以这个地方的顺序只能是(val, key)

23.合并k个升序链表

class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        import heapq
        heap = []
        dummy = ListNode(0)
        cur = dummy
        for index, l in enumerate(lists):
            if l:
                heapq.heappush(heap, (l.val, index, l))
        while heap:
            val, index, l = heapq.heappop(heap)
            cur.next = l
            cur = cur.next
            if l.next:
                l = l.next
                heapq.heappush(heap, (l.val, index, l))
        return dummy.next

易错总结

  • line8:要加if l判断,否则通不过测试用例:[[]]
  • line14:if l.next,判断条件注意

链表反转

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        pre = None
        cur = head
        while cur:
            nex = cur.next
            cur.next = pre
            pre = cur
            cur = nex
        return pre

易错总结

  • pre的初始化是 = None,而不是常见的dummy方式,常见的dummy = ListNode(0)的话,链表第一个元素反转后就不能指向None了
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(0, head)
        pre = dummy
        while pre.next and pre.next.next:
            cur = pre.next
            nex = pre.next.next

            cur.next = nex.next
            nex.next = cur
            pre.next = nex

            pre = pre.next.next
        return dummy.next

易错总结

  • 条件while pre.next and pre.next.next,从pre角度出发,循环内部更新pre=pre.next.next
  • 通过pre->1(cur)->2(nex)->3理解步骤
    • 第一步:cur.next = nex.next,1->3,把1处理好了,然后可以处理1的前面元素2
    • 第二步:nex.next = cur,2->1,把2处理好了,然后可以处理2的前面元素pre
    • 第三步:pre.next = nex,pre->2,把pre处理好
    • 第四步:pre = pre.next.next,pre向后挪两步

你可能感兴趣的:(算法,数据结构,python)