Skip to content

Commit d8c2d3b

Browse files
committed
2.25
1 parent dd46347 commit d8c2d3b

10 files changed

+232
-11
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
- [合并两个排序的链表](./SwordToOffer/Doc/合并两个排序的链表.md)
1818
- [***树的子结构***](./SwordToOffer/Doc/树的子结构.md)
1919
- [***树的镜像***](./SwordToOffer/Doc/树的镜像.md)
20-
- [顺时针打印矩阵](./SwordToOffer/Doc/顺时针打印矩阵.md)
20+
- [***顺时针打印矩阵***](./SwordToOffer/Doc/顺时针打印矩阵.md)
21+
- [***包含min函数的栈***](./SwordToOffer/Doc/包含min函数的栈.md)
22+
- [栈的压入&弹出顺序](./SwordToOffer/Doc/栈的压入&弹出顺序.md)
23+
- [从上往下打印二叉树](./SwordToOffer/Doc/从上往下打印二叉树.md)
24+
- [二叉搜索树的后序遍历](./SwordToOffer/Doc/二叉搜索树的后序遍历.md)
25+
-
2126

2227

2328

SwordToOffer/Code/18.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ class Solution:
99
def printMatrix(self, matrix):
1010
# write code here
1111
size = len(matrix)
12-
size = int(np.sqrt(size))
13-
matrix = np.reshape(np.array(matrix), newshape=(size, size))
1412

1513
newArray = []
14+
if size==1:
15+
newArray.append(matrix[0][0])
16+
return newArray
1617

17-
for index1 in range(0, int(size / 2)):
18+
for index1 in range(0, size):
1819
for index2 in range(index1, size - index1 - 1):
19-
newArray.append(matrix[index1, index2])
20+
newArray.append(matrix[index1][ index2])
2021
for index3 in range(index1, size - index1 - 1):
21-
newArray.append(matrix[index3, size - index1 - 1])
22+
newArray.append(matrix[index3][ size - index1 - 1])
2223
for index4 in range(size - index1 - 1, index1, -1):
23-
newArray.append(matrix[size - index1 - 1, index4])
24+
newArray.append(matrix[size - index1 - 1][ index4])
2425
for index5 in range(size - index1 - 1, index1, -1):
25-
newArray.append(matrix[index5, index1])
26+
newArray.append(matrix[index5][ index1])
2627

2728
return newArray
2829

@@ -32,15 +33,14 @@ def reversematrix(self,matrix):
3233
for i in range(s[1]-1,-1,-1):
3334
a=[]
3435
for j in range(s[0]):
35-
a.append(matrix[j,i])
36+
a.append(matrix[j][i])
3637
newmatrix.append(a)
3738
newmatrix=np.array(newmatrix)
3839
return newmatrix
3940

4041
def printMatrix2(self, matrix):
4142
newArray=[]
4243
matrix=np.array(matrix)
43-
print matrix
4444
while len(matrix)!=0:
4545
newArray.extend(matrix[0])
4646
matrix=matrix[1:]
@@ -49,4 +49,5 @@ def printMatrix2(self, matrix):
4949

5050

5151
s = Solution()
52-
s.printMatrix2([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
52+
#print s.printMatrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
53+
print s.printMatrix([[1]])

SwordToOffer/Code/19.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def __init__(self):
4+
self.a = []
5+
self.b = []
6+
self.count = 0
7+
8+
def push(self, node):
9+
# write code here
10+
if self.count == 0:
11+
self.b.append(node)
12+
self.count += 1
13+
14+
elif node < self.b[-1]:
15+
self.b.append(node)
16+
self.count += 1
17+
else:
18+
self.b.append(self.b[-1])
19+
self.count += 1
20+
self.a.append(node)
21+
22+
def pop(self):
23+
# write code here
24+
r = self.a[-1]
25+
self.a.pop()
26+
self.b.pop()
27+
self.count -= 1
28+
return r
29+
30+
def top(self):
31+
# write code here
32+
return self.a[-1]
33+
34+
def min(self):
35+
# write code here
36+
return self.b[-1]
37+
38+
39+
a = ["PSH3", "MIN", "PSH4", "MIN", "PSH2", "MIN", "PSH3", "MIN", "POP", "MIN", "POP", "MIN", "POP", "MIN", "PSH0",
40+
"MIN"]
41+
42+
s = Solution()
43+
for item_a in a:
44+
s.push(item_a)
45+
46+
for i in range(len(a)):
47+
print s.min()
48+
s.pop()

SwordToOffer/Code/20.py

Whitespace-only changes.

SwordToOffer/Code/21.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding:utf-8 -*-
2+
class TreeNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
class Solution:
9+
def __init__(self):
10+
self.s=[]
11+
12+
# 返回从上到下每个节点值列表,例:[1,2,3]
13+
def PrintFromTopToBottom(self, root):
14+
# write code here
15+
a = []
16+
if root is None:
17+
return a
18+
19+
self.s.append(root)
20+
a.append(root.val)
21+
while self.s:
22+
t=self.s[0]
23+
del self.s[0]
24+
if t.left is not None:
25+
self.s.append(t.left)
26+
a.append(t.left.val)
27+
if t.right is not None:
28+
self.s.append(t.right)
29+
a.append(t.right.val)
30+
31+
return a
32+
33+
34+
35+
pRoot8 = TreeNode(8)
36+
treeNode6 = TreeNode(6)
37+
treeNode10 = TreeNode(10)
38+
treeNode5 = TreeNode(5)
39+
treeNode7 = TreeNode(7)
40+
treeNode9 = TreeNode(9)
41+
treeNode11 = TreeNode(11)
42+
43+
pRoot8.left = treeNode6
44+
pRoot8.right = treeNode10
45+
treeNode6.left = treeNode5
46+
treeNode6.right = treeNode7
47+
treeNode10.left = treeNode9
48+
treeNode10.right = treeNode11
49+
50+
s=Solution()
51+
print s.PrintFromTopToBottom(pRoot8)

SwordToOffer/Code/22.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def __init__(self):
4+
self.stk=[]
5+
6+
def VerifySquenceOfBST(self, sequence):
7+
# write code here
8+
a=[]
9+
flag=0
10+
for i in range(len(sequence)):
11+
if sequence[i]<sequence[i+1] :
12+
if flag==0:
13+
flag=1
14+
else:
15+
16+
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 二叉搜索树的后序遍历
2+
3+
<center>知识点:</center>
4+
5+
6+
## 题目描述
7+
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
8+
## 解题思路
9+
首先要明确什么是二叉搜索树:
10+
它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。
11+
12+
13+
## 代码
14+
15+
[这里](../Code/n.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 从上往下打印二叉树
2+
3+
<center>知识点:二叉树</center>
4+
5+
6+
## 题目描述
7+
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
8+
## 解题思路
9+
利用队列的思想,刚开始将根节点入队(如果根节点不为空),然后开始循环,每个循环从队列中取出第一个作为当前元素,对每一个元素,先将其遍历,然后将其左节点、右节点顺序入队(如果不为空),以此类推。
10+
11+
12+
## 代码
13+
14+
[这里](../Code/21.py)
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 包含min函数的栈
2+
3+
<center>知识点:栈</center>
4+
5+
6+
## 题目描述
7+
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
8+
## 解题思路
9+
使用两个数组,第一个数组用来实现栈先入后出的功能(记为数组A),第二个栈实现存储当前栈最小值的功能(记为数组B),具体做法是,每来
10+
一个待入栈的元素,将其先入数组A,然后将其与数组B的最后一个元素即B栈的栈顶比较,如果当前元素比B栈栈顶小,则将其入B栈,否则将B栈的倒数第一个再入一次栈,弹栈时A和B同时弹,这样可以保证B栈的当前栈顶一定是A栈中的最小元素,而且避免了弹栈时A和B栈元素个数不一样的情况。
11+
举个例子:元素为[7,3,4,8,7,1,10],那么A个B栈的元素应该是:
12+
A:[7,3,4,8,7,1,10]
13+
B:[7,3,3,3,3,1,1]
14+
这样弹栈顺序和对应时刻min值为:
15+
弹栈:10,1,7,8,4,3,7
16+
min:1,1,3,3,3,3,7
17+
## 代码
18+
```python
19+
class Solution:
20+
def __init__(self):
21+
self.a = []
22+
self.b = []
23+
self.count = 0
24+
25+
def push(self, node):
26+
# write code here
27+
if self.count == 0:
28+
self.b.append(node)
29+
self.count += 1
30+
31+
elif node < self.b[-1]:
32+
self.b.append(node)
33+
self.count += 1
34+
else:
35+
self.b.append(self.b[-1])
36+
self.count += 1
37+
self.a.append(node)
38+
39+
def pop(self):
40+
# write code here
41+
r = self.a[-1]
42+
self.a.pop()
43+
self.b.pop()
44+
self.count -= 1
45+
return r
46+
47+
def top(self):
48+
# write code here
49+
return self.a[-1]
50+
51+
def min(self):
52+
# write code here
53+
return self.b[-1]
54+
55+
```
56+
[这里](../Code/19.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 栈的压入&弹出顺序
2+
3+
<center>知识点:栈</center>
4+
5+
6+
## 题目描述
7+
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
8+
## 解题思路
9+
10+
11+
12+
## 代码
13+
14+
[这里](../Code/20.py)

0 commit comments

Comments
 (0)