Skip to content

Commit bb61156

Browse files
committed
New leetcode
1 parent 65f6a62 commit bb61156

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+681
-0
lines changed
Loading

CLRS/.DS_Store

6 KB
Binary file not shown.

CLRS/BLACKBOARD.txt

Whitespace-only changes.

CLRS/BST_insert.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# coding=utf-8
2+
3+
def insert(head, x):
4+
"""
5+
:二分搜索树插入函数的实现
6+
:type head: 二分搜索树头节点
7+
:type x: 需要插入的树的节点
8+
"""
9+
fir, sec = head, None
10+
11+
# 搜索确定x应该安放的具体位置, fir最后指向的None
12+
# 即为最后的应安放的位置
13+
14+
while fir != None:
15+
sec = fir
16+
if x.val < fir.val:
17+
fir = fir.left
18+
else: fir = fir.right
19+
20+
# 第一种判定是否树本身为空
21+
# 第二种判定x值小于sec
22+
# 第三种判定x值大于sec
23+
# 对插入节点进行插入判断和安置
24+
25+
if sec == None:
26+
head = x
27+
elif sec.val > x.val:
28+
sec.left = x
29+
else:
30+
sec.right = x
31+
32+
return head # 返回头节点

CLRS/CS 180 Textbook.pdf

4.84 MB
Binary file not shown.

CLRS/LCS.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def findLCS(A, n, B, m):
2+
# write code here
3+
c = [([0] * (m + 1)) for _ in xrange(n + 1)]
4+
for i in xrange(n):
5+
for j in xrange(m):
6+
if A[i] == B[j]:
7+
c[i + 1][j + 1] = c[i][j] + 1
8+
elif c[i][j + 1] > c[i + 1][j]:
9+
c[i + 1][j + 1] = c[i][j + 1]
10+
else:
11+
c[i + 1][j + 1] = c[i + 1][j]
12+
return c[n][m]
13+
14+
15+
print findLCS("1A2C3D4B56",10,"B1D23CA45B6A",12)

CLRS/RB-STD.txt

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
LF-RT(T, x)
2+
y = x.right
3+
x.right = y.left
4+
if y.left != T.nil
5+
y.left.parent = x
6+
y.parent = x.parent
7+
if x.parent == T.nil
8+
T.root = y
9+
elif x.parent.right = x
10+
x.parent.right = y
11+
else
12+
x.parent.left = y
13+
y.left = x
14+
x.parent = y
15+
16+
RB-TRANSPLANT(T, u, v)
17+
if u.parent == T.nil
18+
T.root = v
19+
elif u.parent.right == u
20+
u.parent.right = v
21+
else
22+
u.parent.left = v
23+
v.parent = u.parent
24+
25+
RB-INSERT(T, z)
26+
x = T.root
27+
y = T.nil
28+
while x != T.nil
29+
y = x
30+
if x.val < z.val
31+
x = x.right
32+
else
33+
x = x.left
34+
z.parent = y.parent
35+
if y == T.nil
36+
T.root = z
37+
elif y.parent.right == y
38+
y.parent.right = z
39+
else
40+
y.parent.left = z
41+
z.left = T.nil
42+
z.right = T.nil
43+
z.color = RED
44+
RB-INSERT-FIX(T, z)
45+
46+
RB-INSERT-FIX(T, z)
47+
while z.parent.color == RED
48+
if z.parent = z.parent.parent.left
49+
y = z.parent.parent.right
50+
if y.color == RED
51+
y.color = BLACK
52+
z.parent.color = BLACK
53+
z.parent.parent.color = RED
54+
z = z.parent.parent
55+
elif z = z.parent.right
56+
z = z.parent
57+
LF-RT(T, z)
58+
z.parent.color = BLACK
59+
z.parent.parent.color = RED
60+
RT-RT(T, z.parent.parent)
61+
else (change left and right)
62+
T.root.color = BLACK
63+
64+
RB-DELETE(T, z)
65+
y = z
66+
y-ori-color = z.color
67+
if z.right == T.nil
68+
x = z.right
69+
TRANSPLANT(T, z, z.right)
70+
elif z.left == T.nil
71+
x = z.left
72+
TRANSPLANT(T, z, z.left)
73+
else
74+
y = TREE-MIN(T, z.right)
75+
y-ori-color = y.color
76+
x = y.right
77+
if y.parent == z
78+
x.parent = y
79+
else
80+
TRANSPLANT(T, y, y.right)
81+
y.right = z.right
82+
y.right.parent = y
83+
TRANSPLANT(T, z, y)
84+
y.left = z.left
85+
y.left.parent = y
86+
y.color = z.color
87+
if y-ori-color == BLACK
88+
RB-DELETE-FIX(T, x)
89+
90+
RB-DELETE-FIX(T, x)
91+
while T.root != x and x.color == BLACK
92+
if x == x.parent.left
93+
w = x.parent.right
94+
if w.color == RED
95+
w.color = BLACK
96+
x.parent.color = RED
97+
LF.RT(x.parent)
98+
w = x.parent.right
99+
if w.left.color == BLACK and w.right.color == BLACK
100+
w.color = RED
101+
x = x.parent
102+
elif w.right.color == BLACK
103+
w.color = RED
104+
w.left.color = BLACK
105+
RT-RT(T, w)
106+
w = x.parent.right
107+
w.color = x.parent.color
108+
w.right.color = BLACK
109+
x.parent.color = BLACK
110+
LF-RT(T, x.parent)
111+
T.root = x
112+
else(change left and right)
113+
x.color = BLACK

CLRS/cut-rod.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# coding=utf-8
2+
3+
# 切钢条问题的迭代解
4+
5+
# def cut_rod(price, n):
6+
# """""""""""""""""
7+
# :切钢条问题动态规划O(n^2)解
8+
# :price type: list
9+
# :n type: integer
10+
# """""""""""""""""
11+
# res = [0] * (n + 1)
12+
# for i in xrange(1, n + 1):
13+
# q = -30000 # 此值只是为了代替python最小值
14+
# for j in xrange(1, i + 1):
15+
# q = max(q, price[j] + res[i - j])
16+
# res[i] = q
17+
# return res[n]
18+
19+
20+
if __name__ == '__main__':
21+
price = [0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
22+
print cut_rod(price, 10)

CLRS/red-black-tree.py

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# coding=utf-8
2+
3+
# CLRS中红黑树的Python实现
4+
5+
class TreeNode(object):
6+
def __init__(self, x):
7+
self.val = x
8+
self.left = None
9+
self.right = None
10+
self.color = None
11+
self.parent = None
12+
13+
class Red_Black_Tree(object):
14+
def __init__(self):
15+
"""""""""
16+
:构造函数
17+
:root作为头节点指针
18+
:dummy作为尾部空节点存在
19+
"""""""""
20+
self.root = None
21+
self.dummy = TreeNode(None)
22+
self.dummy.color = 'Black'
23+
24+
def insert(self, z):
25+
"""""""""
26+
:插入函数
27+
:z type: TreeNode
28+
"""""""""
29+
x = self.root
30+
y = self.dummy
31+
while x != self.dummy:
32+
y = x
33+
if x.val < z.val:
34+
x = x.right
35+
else:
36+
x = x.left
37+
z.parent = y
38+
if y == self.dummy:
39+
self.root = z
40+
elif y.val < z.val:
41+
y.right = z
42+
else:
43+
y.left = z
44+
z.left = self.dummy
45+
z.right = self.dummy
46+
z.color = 'Red'
47+
self.insert_fix(z)
48+
49+
def insert_fix(self, z):
50+
"""""""""
51+
:插入修正函数
52+
:z type: TreeNode
53+
"""""""""
54+
while z.parent.color == 'Red':
55+
if z.parent.parent.left == z.parent:
56+
y = z.parent.parent.right
57+
if y.color == 'Red':
58+
y.color = 'Black'
59+
z.parent.color = 'Black'
60+
z.parent.parent.color = 'Red'
61+
z = z.parent.parent
62+
else:
63+
if z == z.parent.right:
64+
z = z.parent
65+
self.left_rotate(z)
66+
z.parent.color = 'Black'
67+
z.parent.parent.color = 'Red'
68+
self.right_rotate(z.parent.parent)
69+
else:
70+
y = z.parent.parent.left
71+
if y.color == 'Red':
72+
y.color = 'Black'
73+
z.parent.color = 'Black'
74+
z.parent.parent.color = 'Red'
75+
z = z.parent.parent
76+
else:
77+
if z == z.parent.left:
78+
z = z.parent
79+
self.right_rotate(z)
80+
z.parent.color = 'Black'
81+
z.parent.parent.color = 'Red'
82+
self.left_rotate(z.parent.parent)
83+
self.root.color = 'Black'
84+
85+
def delete(self, z):
86+
"""""""""
87+
:删除函数
88+
:z type: TreeNode
89+
"""""""""
90+
y = z
91+
y_ori_color = z.color
92+
if z.left == self.dummy:
93+
x = z.right
94+
self.transplant(z, z.right)
95+
elif z.right == self.dummy:
96+
x = z.left
97+
self.transplant(z, z.left)
98+
else:
99+
y = self.tree_min(z.right) # tree_min函数还没写
100+
y_ori_color = y.color
101+
x = y.right
102+
if y.parent == z:
103+
x.parent = y
104+
else:
105+
self.transplant(y, y.right)
106+
y.right = z.right
107+
y.right.parent = y
108+
self.transplant(z, y)
109+
y.left = z.left
110+
y.left.parent = y
111+
y.color = z.color
112+
if y_ori_color == 'Black':
113+
self.delete_fix(x)
114+
115+
def delete_fix(self, x):
116+
"""""""""
117+
:删除修正函数
118+
:x type: TreeNode
119+
"""""""""
120+
while self.dummy != x and x.color == 'Black':
121+
if x.parent.left == x:
122+
w = x.parent.right
123+
if w.color == 'Red':
124+
w.color = 'Black'
125+
x.parent.color = 'Red'
126+
self.left_rotate(x.parent)
127+
w = x.parent.right
128+
if w.left.color == 'Black' and w.right.color == 'Black':
129+
w.color = 'Red'
130+
x = x.parent
131+
else:
132+
if w.right.color == 'Black':
133+
w.left.color = 'Black'
134+
w.color = 'Red'
135+
self.right_rotate(w)
136+
w = x.parent.right
137+
w.color = x.parent.color
138+
w.right.color = 'Black'
139+
x.parent.color = 'Black'
140+
self.left_rotate(x.parent)
141+
self.root = x
142+
else:
143+
w = x.parent.left
144+
if w.color == 'Red':
145+
w.color = 'Black'
146+
x.parent.color = 'Red'
147+
self.right_rotate(x.parent)
148+
w = x.parent.left
149+
if w.left.color == 'Black' and w.right.color == 'Black':
150+
w.color = 'Red'
151+
x = x.parent
152+
else:
153+
if w.left.color == 'Black':
154+
w.right.color = 'Black'
155+
w.color = 'Red'
156+
self.left_rotate(w)
157+
w = x.parent.left
158+
w.color = x.parent.color
159+
w.left.color = 'Black'
160+
x.parent.color = 'Black'
161+
self.right_rotate(x.parent)
162+
self.root = x
163+
x.color = 'Black'
164+
165+
def transplant(self, u, v):
166+
"""""""""
167+
:移植函数
168+
:u type: TreeNode
169+
:v type: TreeNode
170+
"""""""""
171+
if u.parent == self.dummy:
172+
self.root = v
173+
elif u.parent.left == u:
174+
u.parent.left = v
175+
else:
176+
u.parent.right = v
177+
v.parent = u.parent
178+
179+
180+
def left_rotate(self, z):
181+
"""""""""
182+
:左转函数
183+
:z type: TreeNode
184+
"""""""""
185+
y = z.right
186+
z.right = y.left
187+
if y.left != None:
188+
y.left.parent = z
189+
y.parent = z.parent
190+
if z.parent == self.dummy:
191+
self.root = y
192+
elif z.parent.left == z:
193+
z.parent.left = y
194+
else:
195+
z.parent.right = y
196+
y.left = z
197+
z.parent = y
198+
199+
if __name__ == '__main__':
200+
a = Red_Black_Tree()
201+
# print a.dummy.color

CLRS/台大课件/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)