Skip to content

Commit 5290b57

Browse files
authored
Add files via upload
1 parent 051f749 commit 5290b57

File tree

16 files changed

+746
-0
lines changed

16 files changed

+746
-0
lines changed

src/chapter1/part2/TimeComplex.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def eat1(n):
2+
for i in range(n):
3+
print("等待1min")
4+
print("等待1min")
5+
print("吃1cm面包")
6+
7+
8+
def eat2(n):
9+
while n > 1:
10+
print("等待1min")
11+
print("等待1min")
12+
print("等待1min")
13+
print("等待1min")
14+
print("吃一半面包")
15+
n /= 2
16+
17+
18+
def eat3(n):
19+
print("等待1min")
20+
print("吃一个鸡腿")
21+
22+
23+
def eat4(n):
24+
for i in range(n):
25+
for j in range(i):
26+
print("等待1min")
27+
print("吃1cm面包")
28+
29+
30+
eat4(16)

src/chapter1/part3/SpaceComplex.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def fun1(n):
2+
i = 3
3+
# do something
4+
5+
6+
def fun2(n):
7+
array = [[0] * n]
8+
# do something
9+
10+
11+
def fun3(n):
12+
matrix = [[0] * n] * n
13+
# do something
14+
15+
16+
def fun4(n):
17+
if n > 0:
18+
fun4(n-1)
19+
# do something
20+
21+
22+
fun2(10)

src/chapter2/part1/ListOperator.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
def operate_list():
4+
# 初始化列表
5+
my_list = [3, 1, 2, 5, 4, 9, 7, 2]
6+
7+
# 读取元素
8+
print(my_list[2])
9+
10+
# 更新元素
11+
my_list[3] = 10
12+
print(my_list[3])
13+
14+
# 尾部插入元素
15+
my_list.append(6)
16+
print(my_list)
17+
18+
# 中间插入元素
19+
my_list.insert(5, 11)
20+
print(my_list)
21+
22+
# 删除元素
23+
my_list.remove(6)
24+
print(my_list)
25+
26+
27+
operate_list()

src/chapter2/part1/MyArray.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
class MyArray:
3+
def __init__(self, capacity):
4+
self.array = [None] * capacity
5+
self.size = 0
6+
7+
def insert(self, index, element):
8+
# 判断访问下标是否超出范围
9+
if index < 0 or index > self.size:
10+
print("超出列表实际元素范围!")
11+
# 从右向左循环,逐个元素向右挪一位。
12+
for i in range(self.size-1, -1, -1):
13+
self.array[i+1] = self.array[i]
14+
# 腾出的位置放入新元素
15+
self.array[index] = element
16+
self.size += 1
17+
18+
def insert_v2(self, index, element):
19+
# 判断访问下标是否超出范围
20+
if index < 0 or index > self.size:
21+
print("超出列表实际元素范围!")
22+
# 如果实际元素达到数组容量上线,数组扩容
23+
if self.size >= len(self.array):
24+
self.resize()
25+
# 从右向左循环,逐个元素向右挪一位。
26+
for i in range(self.size-1, -1, -1):
27+
self.array[i+1] = self.array[i]
28+
# 腾出的位置放入新元素
29+
self.array[index] = element
30+
self.size += 1
31+
32+
def resize(self):
33+
array_new = [None] * len(self.array) * 2
34+
# 从旧数组拷贝到新数组
35+
for i in range(self.size):
36+
array_new[i] = self.array[i]
37+
self.array = array_new
38+
39+
def remove(self, index):
40+
# 判断访问下标是否超出范围
41+
if index < 0 or index >= self.size:
42+
print("超出列表实际元素范围!")
43+
# 从左到右,逐个元素向左挪动一位
44+
for i in range(index, self.size):
45+
self.array[i] = self.array[i+1]
46+
self.size -= 1
47+
48+
def output(self):
49+
for i in range(self.size):
50+
print(self.array[i])
51+
52+
53+
array = MyArray(4)
54+
array.insert_v2(0, 10)
55+
array.insert_v2(0, 11)
56+
array.insert_v2(0, 15)
57+
array.remove(1)
58+
array.output()
59+
60+
61+

src/chapter2/part2/MyLinkedList.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None
5+
6+
7+
class LinkedList:
8+
def __init__(self):
9+
self.size = 0
10+
self.head = None
11+
self.last = None
12+
13+
def get(self, index):
14+
if index < 0 or index >= self.size:
15+
print('超出链表节点范围!')
16+
return
17+
p = self.head
18+
for i in range(index):
19+
p = p.next
20+
return p
21+
22+
def insert(self, data, index):
23+
if index < 0 or index > self.size:
24+
print('超出链表节点范围!')
25+
return
26+
node = Node(data)
27+
if self.size == 0:
28+
# 空链表
29+
self.head = node
30+
self.last = node
31+
elif index == 0:
32+
# 插入头部
33+
node.next = self.head
34+
self.head = node
35+
elif self.size == index:
36+
# 插入尾部
37+
self.last.next = node
38+
self.last = node;
39+
else:
40+
# 插入中间
41+
prev_node = self.get(index-1)
42+
node.next = prev_node.next
43+
prev_node.next = node
44+
self.size += 1
45+
46+
def remove(self, index):
47+
if index < 0 or index >= self.size:
48+
print('超出链表节点范围!')
49+
return
50+
# 暂存被删除的节点,用于返回
51+
if index == 0:
52+
# 删除头节点
53+
removed_node = self.head
54+
self.head = self.head.next
55+
elif index == self.size - 1:
56+
# 删除尾节点
57+
prev_node = self.get(index-1)
58+
removed_node = prev_node.next
59+
prev_node.next = None
60+
self.last = prev_node
61+
else:
62+
# 删除中间节点
63+
prev_node = self.get(index-1)
64+
next_node = prev_node.next.next
65+
removed_node = prev_node.next
66+
prev_node.next = next_node
67+
self.size -= 1
68+
return removed_node
69+
70+
def output(self):
71+
p = self.head
72+
while p is not None:
73+
print(p.data)
74+
p = p.next
75+
76+
77+
linkedList = LinkedList()
78+
linkedList.insert(3, 0)
79+
linkedList.insert(4, 0)
80+
linkedList.insert(9, 2)
81+
linkedList.insert(5, 3)
82+
linkedList.insert(6, 1)
83+
linkedList.remove(0)
84+
linkedList.output()

src/chapter2/part3/MyQueue.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
class MyQueue:
3+
def __init__(self, capacity):
4+
self.list = [None] * capacity
5+
self.front = 0
6+
self.rear = 0
7+
8+
def enqueue(self, element):
9+
if (self.rear+1) % len(self.list) == self.front:
10+
print('队列已满!')
11+
return
12+
self.list[self.rear] = element
13+
self.rear = (self.rear+1) % len(self.list)
14+
15+
def dequeue(self):
16+
if self.rear == self.front:
17+
print('队列为空!')
18+
return
19+
dequeue_element = self.list[self.front]
20+
self.front = (self.front+1) % len(self.list)
21+
return dequeue_element
22+
23+
def output(self):
24+
i = self.front
25+
while i != self.rear:
26+
print(self.list[i])
27+
i = (i+1) % len(self.list)
28+
29+
30+
myQueue = MyQueue(6)
31+
myQueue.enqueue(3)
32+
myQueue.enqueue(5)
33+
myQueue.enqueue(6)
34+
myQueue.dequeue()
35+
myQueue.dequeue()
36+
myQueue.enqueue(2)
37+
myQueue.enqueue(4)
38+
myQueue.output()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
3+
class TreeNode:
4+
def __init__(self, data):
5+
self.data = data
6+
self.left = None
7+
self.right = None
8+
9+
10+
def create_binary_tree(input_list=[]):
11+
if input_list is None or len(input_list) == 0:
12+
return None
13+
data = input_list.pop(0)
14+
if data is None:
15+
return None
16+
node = TreeNode(data)
17+
node.left = create_binary_tree(input_list)
18+
node.right = create_binary_tree(input_list)
19+
return node
20+
21+
22+
def pre_order_traversal(node):
23+
if node is None:
24+
return
25+
print(node.data)
26+
pre_order_traversal(node.left)
27+
pre_order_traversal(node.right)
28+
return node
29+
30+
31+
def in_order_traversal(node):
32+
if node is None:
33+
return
34+
in_order_traversal(node.left)
35+
print(node.data)
36+
in_order_traversal(node.right)
37+
return node
38+
39+
40+
def post_order_traversal(node):
41+
if node is None:
42+
return
43+
post_order_traversal(node.left)
44+
post_order_traversal(node.right)
45+
print(node.data)
46+
return node
47+
48+
49+
my_input_list = list([3, 2, 9, None, None, 10, None, None, 8, None, 4])
50+
root = create_binary_tree(my_input_list)
51+
print("前序遍历:")
52+
pre_order_traversal(root)
53+
print("中序遍历:")
54+
in_order_traversal(root)
55+
print("后序遍历:")
56+
post_order_traversal(root)
57+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from queue import Queue
2+
3+
4+
def level_order_traversal(node):
5+
queue = Queue()
6+
queue.put(node)
7+
while not queue.empty():
8+
node = queue.get()
9+
print(node.data)
10+
if node.left is not None:
11+
queue.put(node.left)
12+
if node.right is not None:
13+
queue.put(node.right)
14+
15+
16+
def create_binary_tree(input_list=[]):
17+
if input_list is None or len(input_list) == 0:
18+
return None
19+
data = input_list.pop(0)
20+
if data is None:
21+
return None
22+
node = TreeNode(data)
23+
node.left = create_binary_tree(input_list)
24+
node.right = create_binary_tree(input_list)
25+
return node
26+
27+
28+
class TreeNode:
29+
def __init__(self, data):
30+
self.data = data
31+
self.left = None
32+
self.right = None
33+
34+
35+
my_input_list = list([3, 2, 9, None, None, 10, None, None, 8, None, 4])
36+
root = create_binary_tree(my_input_list)
37+
print("层序遍历:")
38+
level_order_traversal(root)
39+
40+
41+
42+

0 commit comments

Comments
 (0)