defsmallest_k(nums, k):
front = nums[:k]
after = nums[k:]# 对前k个数建立最大堆for i inrange(k //2,-1,-1):
heapify(front, i, k)# 从after中依次取出数据和堆顶比for i in after:if i < front[0]:
front[0]= i
heapify(front,0, k)return front
defheapify(nums, i, size):"""调整大顶堆"""
left, right =2*i+1,2*i+2# 第i号结点的左子树和右子树
largest = i
if left < size and nums[left]> nums[largest]:
largest = left
if right < size and nums[right]> nums[largest]:
largest = right
if largest != i:# 若发现左右子树比父结点大, 则互换位置, 再继续向下调整堆
swap(nums, i, largest)
heapify(nums, largest, size)
classListNode():def__init__(self, val=0):
self.val = val
self.next=None
(1) 反转链表
defreverse_list(head):
cur = head
pre =Nonewhile cur:
nxt = cur.next
cur.next= pre
pre = cur
cur = nxt
return pre
(2) 链表是否有环
defhas_cycle(head):
p1 = p2 = head
while p2 and p2.next:
p1 = p1.next
p2 = p2.next.nextif p1 is p2:returnTruereturnFalse
(3) 合并两个升序链表
defmerge_two_lists(l1, l2):
head = ListNode()
cur = head
while l1 and l2:if l1.val < l2.val:
cur.next= l1
l1 = l1.nextelse:
cur.next= l2
l2 = l2.next
cur = cur.next
cur.next= l1 or l2
return head.next
(4) 合并k个升序链表
defmerge_k_lists(lists):
all_val =[]for l in lists:while l:
all_val.append(l.val)
l = l.next
head = ListNode()
cur = head
import heapq
heapq.heapify(all_val)while all_val:
val = heapq.heappop(all_val)
cur.next= ListNode(val)
cur = cur.nextreturn head.next
classTreeNode():def__init__(self, val=0):
self.val = left
self.left =None
self.right =None
(1) 先序遍历
defpre_order_trav(root):"""根左右"""
stack =[]
ret =[]
cur = root
while stack or cur:if cur:
ret.append(cur.val)
stack.append(cur.right)
cur = cur.left
else:
cur = stack.pop()return ret
(2) 中序遍历
defin_order_trav(root):"""左根右"""
stack =[]
ret =[]
cur = root
while cur or stack:if cur:
stack.append(cur)
cur = cur.left
else:
cur = stack.pop()
ret.append(cur.val)
cur = cur.right
return ret
(3) 后序遍历
classpost_order_trav(root):"""根右左 -> 左右根"""
stack =[]
ret =[]
cur = root
while cur or stack:if cur:
ret.append(cur.val)
stack.append(cur.left)
cur = cur.right
else:
cur = stack.pop()return ret[::-1]
(4) 层序遍历
deflevel_order_trav(root):
queue =[root]
ret =[]while queue:
parent = queue.pop(0)
ret.append(parent.val)if parent.left:
queue.append(parent.left)if parent.right:
queue.append(parent.right)return ret
defselect_sort(nums):for i inrange(len(nums)-1):
min_idx = i
for j inrange(i+1,len(nums)):if nums[j]< nums[min_idx]:
min_idx = j
swap(nums, i, min_idx)return nums
(4) 堆排序
defheapify(nums, i, size):
left =2*i +1
right =2*i +2
largest = i
if left < size and nums[left]> nums[largest]:
largest = left
if right < size and nums[right]> nums[largest]:
largest = right
if largest != i:
swap(nums, i, largest)
heapify(nums, largest, size)defheap_sort(nums):# 建立最大堆
size =len(nums)for i inrange(size //2,-1,-1):
heapify(nums, i, size)# 每一轮把堆顶放到后面for i inrange(len(nums)-1,0,-1):
swap(nums,0, i)
size -=1
heapify(nums,0, size)return nums
(5) 插入排序
definsert_sort(nums):for i inrange(1,len(nums)):
val = nums[i]
j = i -1while j >=0and nums[j]> val:
nums[j+1]= nums[j]
j -=1
nums[j+1]= val
return nums
(6) 希尔排序
defshell_sort(nums):
n =len(nums)
gap = n //2while gap >=2:for i inrange(gap):
j = i
while j+gap < n:if nums[j]> nums[j+gap]:
swap(nums, j, j+gap)
j += gap
gap //=2return nums
(7) 归并排序
defmerge(left, right):
ret =[]while left and right:if left[0]< right[0]:
ret.append(left.pop(0))else:
ret.append(right.pop(0))
ret.extend(left or right)return ret
defmerge_sort(nums):iflen(nums)<2:return nums
mid =len(nums)//2
left = nums[:mid]
right = nums[mid:]return merge(merge_sort(left), merge_sort(right))