![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python数据结构与算法
专职
人生苦短,我用python
展开
-
python单链表实现头部添加、尾部添加、打印节点、反转链表等
python实现链表原创 2022-06-13 14:05:31 · 537 阅读 · 1 评论 -
python中通过两个队列实现一个栈
import queueclass Stack(object): """两个队列生成一个栈""" def __init__(self): self.master_queue = queue.Queue() self.minor_queue = queue.Queue() def push(self, value): self.master_queue.put(value) def pop(self): .原创 2021-07-27 10:13:43 · 321 阅读 · 0 评论 -
python中链表的逆置
# 构建链表class Node: def __init__(self, value, next=None): self.value = value self.next = nexthead = Node("head")latest = headfor x in range(5): node = Node("v-{}".format(x)) latest.next = node latest = node# print(he.原创 2021-07-26 18:06:56 · 392 阅读 · 0 评论 -
python实现斐波那契数列
nitems = int(input("请输入您需要几项"))n1 = 0n2 = 1count = 2if nitems <= 0: print("请输入一个正整数")elif nitems == 1: print("斐波那切数列:") print(n1)else: print("斐波那切数列:") print(n1, ",", n2, end=", ") while count < nitems: nth = .原创 2021-07-12 11:06:46 · 201 阅读 · 0 评论 -
python中的数据结构树与二叉树
1. 树的实例:模拟文件系统class Node: """树这种结构大部分都是通过链式存储的""" def __init__(self, name, type="dir"): self.name = name self.type = type # "dir" or "file" self.children = [] self.parent = None def __repr__(self): .原创 2021-07-03 16:12:10 · 131 阅读 · 0 评论 -
python中的数据结构链表、哈希表
1. 链表(1)单链表实现class Node(object): def __init__(self, item): self.item = item self.next = Nonedef create_linklist_head(li): """单链表头插法""" head = Node(li[0]) for element in li[1:]: node = Node(element) # 创建节点原创 2021-07-03 12:13:24 · 560 阅读 · 0 评论 -
python中的数据结构栈和队列
1. 数据结构的分类2. 实现一个栈代码:class Stack(object): """使用python中的列表实现一个栈""" def __init__(self): # 构造栈 self.stack = [] def push(self, element): # 进展/压栈 self.stack.append(element) def pop(self): # 出.原创 2021-06-27 10:54:47 · 166 阅读 · 0 评论 -
python实现几种其它排序算法
1. 希尔排序def insert_sort_gap(li, gap): for x in range(gap, len(li)): # x:表示摸到牌的下标 tmp = li[x] # tmp:存储摸到牌的值 y = x - gap # y表示手里牌的最大下标 while y >= 0 and li[y] > tmp: li[y + gap] = li[y] y -= gap原创 2021-06-26 12:08:28 · 140 阅读 · 0 评论 -
python实现几种排序算法
1. 排序算法的种类原创 2021-06-19 17:03:56 · 352 阅读 · 5 评论 -
python中线性查找和二分查找代码实现
from datetime import datetime, timedelta# 返回日期、时间、年、月、日、时、分、秒、星期的两种, 以及日期的加减法v1 = datetime.now().date()v2 = datetime.now().time()v3 = datetime.now().yearv4 = datetime.now().monthv5 = datetime.now().dayv6 = datetime.now().hourv7 = datetime.now()..原创 2021-06-19 12:50:24 · 160 阅读 · 0 评论 -
python中的递归讲解
1. 递归需要满足两点(1)调用函数自己(2)需要有条件退出程序2. 代码def hello(x): if x > 0: print(x) hello(x - 1)hello(10)原创 2021-06-19 11:22:03 · 206 阅读 · 0 评论 -
时间复杂度和空间复杂度
O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n)如图:原创 2020-08-09 12:22:48 · 1124 阅读 · 0 评论 -
python中冒泡排序算法实现
原创 2020-08-07 10:37:54 · 76 阅读 · 0 评论 -
python中计时模块timeit的使用方法
列表追加追加元素或列表的三种方式:append extend +=import timeitdef test1(): li = [] for x in range(10000): li.append(x)def test2(): li = [] for x in range(10000): li = li + [x]def test3(): li = [] for x in range(10000):.原创 2020-08-06 14:36:00 · 563 阅读 · 0 评论