Skip to content

Commit cd4410e

Browse files
committed
2.26
1 parent d8c2d3b commit cd4410e

13 files changed

+417
-11
lines changed

README.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
11
# 牛客网-剑指offer
22

33
- [二维数组中的查找](./SwordToOffer/Doc/二维数组中的查找.md)
4+
45
- [替换空格](./SwordToOffer/Doc/替换空格.md)
6+
57
- [从尾到头打印链表](./SwordToOffer/Doc/从尾到头打印链表.md)
8+
69
- [重建二叉树](./SwordToOffer/Doc/重建二叉树.md)
10+
711
- [用两个栈实现队列](./SwordToOffer/Doc/用两个栈实现队列.md)
12+
813
- [旋转数组的最小数字](./SwordToOffer/Doc/旋转数组的最小数字.md)
14+
915
- [跳台阶](./SwordToOffer/Doc/跳台阶.md)
16+
1017
- [变态跳台阶](./SwordToOffer/Doc/变态跳台阶.md)
18+
1119
- [***矩形覆盖***](./SwordToOffer/Doc/矩形覆盖.md)
20+
1221
- [二进制中1的个数](./SwordToOffer/Doc/二进制中1的个数.md)
22+
1323
- [***数值的整数次方***](./SwordToOffer/Doc/数值的整数次方.md)
24+
1425
- [调整数组顺序使奇数位于偶数前面](./SwordToOffer/Doc/调整数组顺序使奇数位于偶数前面.md)
26+
1527
- [链表中倒数第k个节点](./SwordToOffer/Doc/链表中倒数第k个节点.md)
28+
1629
- [反转链表](./SwordToOffer/Doc/反转链表.md)
30+
1731
- [合并两个排序的链表](./SwordToOffer/Doc/合并两个排序的链表.md)
32+
1833
- [***树的子结构***](./SwordToOffer/Doc/树的子结构.md)
34+
1935
- [***树的镜像***](./SwordToOffer/Doc/树的镜像.md)
36+
2037
- [***顺时针打印矩阵***](./SwordToOffer/Doc/顺时针打印矩阵.md)
38+
2139
- [***包含min函数的栈***](./SwordToOffer/Doc/包含min函数的栈.md)
40+
2241
- [栈的压入&弹出顺序](./SwordToOffer/Doc/栈的压入&弹出顺序.md)
42+
2343
- [从上往下打印二叉树](./SwordToOffer/Doc/从上往下打印二叉树.md)
24-
- [二叉搜索树的后序遍历](./SwordToOffer/Doc/二叉搜索树的后序遍历.md)
25-
-
44+
45+
- [***二叉搜索树的后序遍历***](./SwordToOffer/Doc/二叉搜索树的后序遍历.md)
46+
47+
- [二叉树中和为某一数的路径](./SwordToOffer/Doc/二叉树中和为某一数的路径.md)
48+
49+
- [***复杂链表的复制***](./SwordToOffer/Doc/复杂链表的复制.md)
50+
51+
- [二叉搜索树与双向链表](./SwordToOffer/Doc/二叉搜索树与双向链表.md)
52+
53+
- [字符串的排列](./SwordToOffer/Doc/字符串的排列.md)
54+
55+
2656

2757

2858

SwordToOffer/Code/20.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def IsPopOrder(self, pushV, popV):
4+
# write code here
5+
size = len(pushV)
6+
popIndex = 0
7+
a = []
8+
for i in range(size):
9+
if pushV[i] == popV[popIndex]:
10+
popIndex += 1
11+
else:
12+
a.append(pushV[i])
13+
14+
if len(a)==0:
15+
return True
16+
17+
if popIndex >= size:
18+
return False
19+
else:
20+
size2 = len(a)
21+
if size2 != size - popIndex:
22+
return False
23+
else:
24+
flag = True
25+
for i in range(size2):
26+
if a[i] != popV[- (i+1)]:
27+
flag = False
28+
break
29+
else:
30+
popIndex += 1
31+
return flag
32+
33+
34+
s = Solution()
35+
print s.IsPopOrder([1], [1])

SwordToOffer/Code/22.py

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
# -*- coding:utf-8 -*-
22
class Solution:
33
def __init__(self):
4-
self.stk=[]
4+
self.stk = []
55

66
def VerifySquenceOfBST(self, sequence):
77
# 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
8+
9+
size = len(sequence)
10+
if sequence is None:
11+
return True
12+
if size==0:
13+
return False
14+
rootNode = sequence[-1]
15+
smallS = []
16+
bigS = []
17+
flag = True
18+
for i in range(size - 1):
19+
if sequence[i] < rootNode:
20+
if flag:
21+
smallS.append(sequence[i])
1422
else:
23+
return False
24+
elif sequence[i] > rootNode:
25+
if flag:
26+
flag = False
27+
bigS.append(sequence[i])
28+
else:
29+
return False
30+
flagS=True
31+
if len(smallS)>0:
32+
flagS=self.VerifySquenceOfBST(smallS)
33+
flagB=True
34+
if len(bigS)>0:
35+
flagB=self.VerifySquenceOfBST(bigS)
1536

1637

38+
return flagS and flagB
1739

40+
s=Solution()
41+
print s.VerifySquenceOfBST([1,2,3,4,5])

SwordToOffer/Code/23.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# -*- coding:utf-8 -*-
2+
import copy
3+
4+
5+
class TreeNode:
6+
def __init__(self, x):
7+
self.val = x
8+
self.left = None
9+
self.right = None
10+
11+
12+
def cmp(a, b):
13+
if len(a) > len(b):
14+
return -1
15+
else:
16+
return 0
17+
18+
19+
class Solution:
20+
21+
# 返回二维列表,内部每个列表表示找到的路径
22+
def FindPath(self, root, expectNumber):
23+
# write code here
24+
if root == None:
25+
return []
26+
27+
if root.val==expectNumber:
28+
return [[1]]
29+
if root.val>expectNumber:
30+
return []
31+
32+
result = []
33+
a = []
34+
root.val = [root.val]
35+
a.append(root)
36+
37+
38+
39+
40+
while a:
41+
cnode = a[0]
42+
del a[0]
43+
44+
45+
if cnode.left != None:
46+
c = sum(cnode.val) + cnode.left.val
47+
if c < expectNumber:
48+
temp = copy.copy(cnode.left.val)
49+
cnode.left.val = copy.copy(cnode.val)
50+
cnode.left.val.append(temp)
51+
a.append(cnode.left)
52+
elif c > expectNumber:
53+
cnode.left = None
54+
elif cnode.left.left == None and cnode.left.right == None:
55+
t = copy.copy(cnode.val)
56+
t.append(cnode.left.val)
57+
result.append(t)
58+
59+
if cnode.right != None:
60+
c = sum(cnode.val)+ cnode.right.val
61+
if c < expectNumber:
62+
temp = copy.copy(cnode.right.val)
63+
cnode.right.val = copy.copy(cnode.val)
64+
cnode.right.val.append(temp)
65+
66+
a.append(cnode.right)
67+
elif c > expectNumber:
68+
cnode.right = None
69+
elif cnode.right.left == None and cnode.right.right == None:
70+
t = copy.copy(cnode.val)
71+
t.append(cnode.right.val)
72+
result.append(t)
73+
74+
result=sorted(result,cmp )
75+
return result
76+
77+
78+
pRoot8 = TreeNode(10)
79+
treeNode1 = TreeNode(5)
80+
treeNode2 = TreeNode(12)
81+
treeNode3 = TreeNode(4)
82+
treeNode4 = TreeNode(7)
83+
treeNode5 = TreeNode(3)
84+
treeNode6 = TreeNode(10)
85+
86+
# pRoot8.left = treeNode1
87+
# pRoot8.right = treeNode2
88+
# treeNode1.left = treeNode3
89+
# treeNode1.right = treeNode4
90+
s = Solution()
91+
print s.FindPath(pRoot8, 10)

SwordToOffer/Code/24.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding:utf-8 -*-
2+
class RandomListNode:
3+
def __init__(self, x):
4+
self.label = x
5+
self.next = None
6+
self.random = None
7+
8+
9+
class Solution:
10+
# 返回 RandomListNode
11+
def Clone(self, pHead):
12+
# write code here
13+
currentNode = pHead
14+
if pHead is None:
15+
return None
16+
17+
while currentNode is not None:
18+
node = RandomListNode(currentNode.label)
19+
node.next = currentNode.next
20+
currentNode.next = node
21+
currentNode = currentNode.next.next
22+
23+
currentNode = pHead
24+
while currentNode is not None:
25+
node = currentNode.next
26+
if currentNode.random is not None:
27+
node.random = currentNode.random.next
28+
currentNode = node.next
29+
30+
currentNode = pHead
31+
cloneP = pHead.next
32+
33+
while currentNode.next is not None:
34+
temp = currentNode.next
35+
currentNode.next = temp.next
36+
currentNode = temp
37+
return cloneP
38+
39+
40+
head = RandomListNode(1)
41+
node2 = RandomListNode(2)
42+
node3 = RandomListNode(3)
43+
node4 = RandomListNode(4)
44+
node5 = RandomListNode(5)
45+
nodex = RandomListNode('#')
46+
47+
head.next = node2
48+
head.random = node3
49+
50+
node2.next = node3
51+
node2.random = node5
52+
53+
node3.next = node4
54+
node3.random = nodex
55+
56+
node4.next = node5
57+
node4.random = node2
58+
59+
node5.random = nodex
60+
61+
s = Solution()
62+
h = s.Clone(head)
63+
a = 0

SwordToOffer/Code/25.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
class Solution:
8+
def Convert(self, pRootOfTree):
9+
# write code here
10+
pass

SwordToOffer/Code/26.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def Permutation(self, ss):
4+
# write code here
5+
if len(ss)==1:
6+
return ss
7+
8+
t = self.Permutation(ss[1:])
9+
10+
11+
s = Solution()
12+
s.Permutation('abcd')
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/25.py)

SwordToOffer/Doc/二叉搜索树的后序遍历.md

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
首先要明确什么是二叉搜索树:
1010
它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。
1111

12+
二叉搜索树的后序遍历一定满足以下条件:
13+
- 最后一个元素是根节点
14+
- 从第一个到第倒数第一个一定可以分为两部分,前一部分都比根节点小,后一部分都比根节点大,且这两部分都是二叉搜索树
15+
16+
根据以上两个特点使用递归即可解决。
1217

1318
## 代码
1419

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 二叉树中和为某一数的路径
2+
3+
<center>知识点:</center>
4+
5+
6+
## 题目描述
7+
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
8+
9+
## 解题思路
10+
11+
从根节点开始,原来的node.val是int类型的值,现在将其变为从根节点到其的路径(list类型),改变完成之后将该list求和,然后与expectNumber比较,如果相等,判断一下当前节点是否是叶子节点(通过判断其左右子树是否都为None),否则返回False,如果小于expectNumber,则继续往后面遍历,依次类推。
12+
13+
![image-20190226143959233](https://ws4.sinaimg.cn/large/006tKfTcgy1g0jtj3b5hnj31ho0gm42y.jpg)
14+
15+
## 代码
16+
17+
[这里](../Code/23.py)

0 commit comments

Comments
 (0)