计数排序

博客地址:https://www.cnblogs.com/zylyehuo/ # _*_coding:utf-8_*_ import random def count_sort(li, max_count=100): count = [0 for _ in range(max_count + 1)]

桶排序

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- import random def bucket_sort(li, n=100, max_num=10000): buckets = [[] for _ in range(n

基数排序

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- # O(n) O(kn) # NB O(nlogn) import random def radix_sort(li): max_num = max(li) # 最大值 9-

栈的应用(后进先出 LIFO)--括号匹配问题

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- class Stack: def __init__(self): self.stack = [] def push(self, element): self.stack.ap

队列的实现方式(先进先出 FIFO)--环形队列

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- class Queue: def __init__(self, size=100): self.queue = [0 for _ in range(size)] self.s

队列的内置模块(deque)--双向队列

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- from collections import deque q = deque([1,2,3,4,5], 5) q.append(6) # 队尾进队 print(q.popl

利用队列的内置模块(deque)模拟 Linux 下的 tail 命令(输出文件中最后几行的内容)

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- from collections import deque def tail(n): # n:指定输出文件中最后几行 with open('test.txt', 'r') a

使用栈解决迷宫问题(深度优先搜索 / 回溯法)

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- maze = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 1, 0, 0, 0, 1, 0, 1], [1, 0, 0, 1, 0

使用队列解决迷宫问题(广度优先搜索 / 最短路径)

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- from collections import deque maze = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 1, 0,

链表的创建&遍历打印

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- class Node: def __init__(self, item): self.item = item self.next = None # 头插法 def creat

哈希表(实现 Python 中的集合 set)

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- class LinkList: class Node: def __init__(self, item=None): self.item = item self.next =

树的实例--模拟文件系统

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- class Node: # 链式存储 def __init__(self, name, type='dir'): self.name = name self.type = t

二叉树的遍历

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- from collections import deque class BiTreeNode: def __init__(self, data): self.data = d

二叉搜索树 / 二叉排序树

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- # 构造二叉树 class BiTreeNode: def __init__(self, data): self.data = data self.lchild = None

贪心算法--找零问题

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- t = [100, 50, 20, 5] def change(t, n): m = [0 for _ in range(len(t))] # m 为各面额纸币的张数 for

贪心算法--背包问题--分数背包

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- stuffs = [(60, 10), (100, 20), (120, 30)] # 每个商品元组表示(价格, 重量) stuffs.sort(key=lambda x:

贪心算法--拼接最大数字问题

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- from functools import cmp_to_key def xy_cmp(x, y): if x + y < y + x: return 1 # 表示 x>y

贪心算法--活动选择问题

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- def activity_selection(a): res = [a[0]] for i in range(1, len(a)): if a[i][0] >= res[-1

动态规划--斐波那契数列

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- # 子问题的重复计算--递归方法--执行效率低 def fibnacci(n): if n == 1 or n == 2: return 1 else: return fib

动态规划--钢条切割问题

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- import time def cal_time(func): def wrapper(*args, **kwargs): t1 = time.time() result =