Skip to content

Commit a99ab38

Browse files
committed
Add 007
1 parent 41fb977 commit a99ab38

File tree

3 files changed

+253
-0
lines changed

3 files changed

+253
-0
lines changed

004二分查询.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
# 实现二分查询
3+
4+
## 什么是二分查询
5+
6+
二分查询是比遍历查询更高效的查询方法,类比现实中的猜数字游戏,如果每次都从中间猜起可以尽可能快速地定位到数据。
7+
8+
注意二分查询要求数据已经排好序。
9+
10+
## 如何实现二分查询
11+
12+
实现二分查询的原理比较简单,先与中间的数比较,如果比它小就在左边查询,如果比它大就在右边查询,而查询方法是一样的,知道无法划分左右。
13+
14+
这种原理可以最直观的是通过递归实现,稍微不太直观的可以用移动数组的左右边界达到同样的目的。
15+
16+
## 使用递归实现
17+
18+
首先创建测试数组和测试数据,并且创建好二分查询的函数。
19+
20+
```
21+
def binary_search(a, x):
22+
return x
23+
24+
25+
a = [1, 2, 3, 5, 55, 66, 77, 88, 99, 100]
26+
x = 2
27+
binary_search(a, x)
28+
```
29+
30+
由于我们要递归实现,因此binary_search的函数其实还需要start和end两个参数,然后就是递归调用。
31+
32+
```
33+
def binary_search(a, x, start, end):
34+
if end < start:
35+
return -1
36+
37+
middle = start + int((end - start) / 2)
38+
39+
if x > a[middle]:
40+
binary_search(a, x, middle + 1, end)
41+
elif x < a[middle]:
42+
binary_search(a, x, start, middle - 1)
43+
else:
44+
return middle
45+
46+
a = [1, 2, 3, 5, 55, 66, 77, 88, 99, 100]
47+
x = 2
48+
49+
binary_search(a, x, 0, len(a)
50+
# Print 1
51+
```
52+
53+
## 使用循环实现
54+
55+
拓展一下,如果想用循环来实现,只需要记录start和end即可,首先初始化这些变量。
56+
57+
```
58+
def binary_search(a, x, start, end):
59+
while start <= end:
60+
middle = start + int((end - start)/2)
61+
if x > a[middle]:
62+
start = middle + 1
63+
elif x < a[middle]:
64+
end = middle - 1
65+
else:
66+
return middle
67+
68+
a = [1, 2, 3, 5, 55, 66, 77, 88, 99, 100]
69+
x = 2
70+
binary_search(a, x, 0, len(a))
71+
# Print 1
72+
```
73+
74+
这是本章内容,希望对你有所帮助。[进入下一章](./005链表.md)

006栈.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
# 实现栈数据结构
3+
4+
## 什么是栈
5+
6+
栈也是非常常见的数据结构,类比现实中的木桶,特点是先进后出,也就是每次插入都在最上面,每次取都取最上面的。
7+
8+
因此栈的操作比较特别,只需要实现push和pop的接口即可。
9+
10+
## 如何实现栈
11+
12+
实际上,Python已经提供了数组,已经提供append和pop接口,能够满足栈的使用场景,用法也简单介绍下。
13+
14+
```
15+
a = [1, 2, 3]
16+
17+
a.append(4)
18+
19+
print(a)
20+
# Print [1, 2, 3, 4]
21+
22+
a.pop()
23+
24+
print(a)
25+
# Print [1, 2, 3]
26+
```
27+
28+
但我们希望自己实现栈这个数据结构,首先是定义Node类和Stack类。
29+
30+
```
31+
class Node(object):
32+
33+
def __init__(self, data, next=None):
34+
self.data = data
35+
self.next = next
36+
37+
class Stack(object):
38+
39+
def __init__(self, top=None):
40+
self.top = top
41+
42+
def is_empty(self):
43+
pass
44+
45+
def push(self, data):
46+
pass
47+
48+
def pop(self):
49+
pass
50+
51+
stack = Stack()
52+
```
53+
54+
然后我们需要去实现push、pop和is_empty这几个函数。其中push和pop需要注意如果top为空的情况。
55+
56+
```
57+
class Stack(object):
58+
59+
def __init__(self, top=None):
60+
self.top = top
61+
62+
def is_empty(self):
63+
if self.top != None:
64+
return True
65+
else:
66+
return False
67+
68+
def push(self, data):
69+
if self.top == None:
70+
self.top = Node(data)
71+
else:
72+
new_node = Node(data)
73+
new_node.next = self.top
74+
self.top = new_node
75+
76+
def pop(self):
77+
if self.top == None:
78+
print("No node in stack")
79+
return
80+
else:
81+
top = self.top
82+
self.top = self.top.next
83+
return top
84+
85+
stack = Stack()
86+
stack.push("node1")
87+
stack.push("node2")
88+
stack.push("node3")
89+
print(stack.pop().data)
90+
# Print "node3"
91+
```
92+
93+
这是本章内容,希望对你有所帮助。[进入下一章](./007队列.md)

007队列.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
# 实现队列数据结构
3+
4+
## 什么是队列
5+
6+
队列和栈类似都是很常见的数据结构,和现实中排队对应,都先进先出的模型。
7+
8+
因此队列这个数据结构,提供的接口是enqueue和dequeue。
9+
10+
## 如何实现
11+
12+
阅读前建议先看看[006栈](./006栈.md)的数据结构介绍,我们首先要定义的Node类和前面定义的是一样的,并且要定义Queue类的基本接口,注意这时应该有left和right两个节点引用(假设是左进右出)。
13+
14+
```
15+
class Node(object):
16+
17+
def __init__(self, data, next=None):
18+
self.data = data
19+
self.next = next
20+
21+
class Queue(object):
22+
23+
def __init__(self, left=None, right=None):
24+
self.left = left
25+
self.right = right
26+
27+
def is_empty(self):
28+
pass
29+
30+
def enqueue(self, data):
31+
pass
32+
33+
def dequeue(self):
34+
pass
35+
```
36+
37+
然后实现这些函数,发现如果每个node之前前后的node会更容易实现些,当然也是牺牲空间换时间。
38+
39+
```
40+
class Node(object):
41+
42+
def __init__(self, data, next=None):
43+
self.data = data
44+
self.next = next
45+
46+
class Queue(object):
47+
48+
def __init__(self, left=None, right=None):
49+
self.left = left
50+
self.right = right
51+
52+
def is_empty(self):
53+
if self.left != None:
54+
return True
55+
else:
56+
return False
57+
58+
def enqueue(self, data):
59+
if self.left == None and self.right == None:
60+
new_node = Node(data)
61+
self.left = new_node
62+
self.right = new_node
63+
else:
64+
new_node = Node(data)
65+
new_node.next = self.left
66+
self.left = new_node
67+
68+
def dequeue(self):
69+
if self.left == None and self.right == None:
70+
print("No node in queue")
71+
return
72+
else:
73+
node = self.right
74+
# TODO: Need to find the right node for self.right
75+
return node
76+
77+
queue = Queue()
78+
queue.enqueue("node1")
79+
queue.enqueue("node2")
80+
queue.enqueue("node3")
81+
print(queue.dequeue().data)
82+
# Print "node1"
83+
```
84+
85+
86+
这是本章内容,希望对你有所帮助。[进入下一章](./007队列.md)

0 commit comments

Comments
 (0)