Skip to content

Commit 3d48a6a

Browse files
committed
更新
1 parent e6fd647 commit 3d48a6a

9 files changed

+295
-65
lines changed

.idea/workspace.xml

Lines changed: 70 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

剑指offer/二叉树的深度.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
题目:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,
3+
最长路径的长度为树的深度。
4+
'''
5+
6+
'''
7+
思路:利用递归实现。如果一棵树只有一个结点,那么它的深度为1。递归的时候无需判断左右子树是否存在,因为如果该节点
8+
为叶节点,它的左右子树不存在,那么在下一级递归的时候,直接return 0。同时,记得每次递归返回值的时候,深度加一操
9+
作,因为计算深度是从根节点下面一个节点开始计算的。
10+
11+
25ms
12+
5632k
13+
'''
14+
15+
# -*- coding:utf-8 -*-
16+
# class TreeNode:
17+
# def __init__(self, x):
18+
# self.val = x
19+
# self.left = None
20+
# self.right = None
21+
class Solution:
22+
def TreeDepth(self, pRoot):
23+
# write code here
24+
if pRoot == None:
25+
return 0
26+
return max(self.TreeDepth(pRoot.left), self.TreeDepth(pRoot.right)) + 1
119 Bytes
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
题目:输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,
3+
输出两个数的乘积最小的
4+
'''
5+
6+
'''
7+
思路:设定两个指针,一个指向数组的起点,一个指向数组的终点,然后对两个数字求和,如果和大于目标值,则把后一个指针前移,
8+
如果和小于目标值,则把前一个指针后移。两个指针交汇的时候如果还没找到,就终止操作。
9+
10+
37ms
11+
5628k
12+
'''
13+
14+
# -*- coding:utf-8 -*-
15+
class Solution:
16+
def FindNumbersWithSum(self, array, tsum):
17+
# write code here
18+
if array == None or len(array) <= 0 or array[-1] + array[-2] < tsum:
19+
return []
20+
start = 0
21+
end = len(array) - 1
22+
while start < end:
23+
if array[start] + array[end] < tsum:
24+
start += 1
25+
elif array[start] + array[end] > tsum:
26+
end -= 1
27+
else:
28+
return [array[start], array[end]]
29+
return []
30+
31+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
题目:小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,
3+
他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,
4+
21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!
5+
'''
6+
7+
'''
8+
思路:设定两个指针,先分别指向数字1和数字2,并设这两个指针为small和big,对small和big求和,如果和大于目标值,
9+
则从当前和中删除small值,并把small值加一,如果和小于目标值,则把big值加一,再把新的big值加入和中。如果和等于
10+
目标值,就输出small到big的序列,同时把big加一并加入和中,继续之前的操作。
11+
12+
举例看起来更好理解一点,虽然是while循环里面没有做small到big之间的连接加减,但是由于都是从相邻出发,加和都是基于
13+
1 2 3相邻数基础之上的加和,大了就对第一个数不断减,小了就对最后一个数不断加即可。
14+
举例: 输入 tsum = 5
15+
1 2
16+
middle = 3 cursum = 3
17+
1 < 3:
18+
3 < 5:
19+
1 2 3
20+
cursum = 1 + 2 + 3 = 6
21+
6 > 5:
22+
cursum - small = 6 - 1 = 5
23+
return 2 3
24+
25+
30ms
26+
5620k
27+
'''
28+
29+
# -*- coding:utf-8 -*-
30+
class Solution:
31+
def FindContinuousSequence(self, tsum):
32+
# write code here
33+
if tsum < 3:
34+
return []
35+
small = 1
36+
big = 2
37+
middle = (tsum + 1) // 2
38+
cursum = big + small
39+
output = []
40+
41+
while small < middle:
42+
if cursum == tsum:
43+
output.append(list(range(small, big + 1)))
44+
while cursum > tsum and small < middle:
45+
cursum -= small
46+
small += 1
47+
if cursum == tsum:
48+
output.append(list(range(small, big + 1)))
49+
big += 1
50+
cursum += big
51+
return output

剑指offer/坐旋转字符串.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'''
2+
题目:汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。
3+
对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后
4+
的结果,即“XYZdefabc”。是不是很简单?OK,搞定它
5+
'''
6+
7+
'''
8+
思路:
9+
10+
'''

剑指offer/平衡二叉树.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
题目:输入一棵二叉树,判断该二叉树是否是平衡二叉树。
3+
'''
4+
5+
'''
6+
平衡二叉树:平衡二叉搜索树(Balanced Binary Tree)具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值
7+
不超过1,并且左右两个子树都是一棵平衡二叉树。
8+
9+
思路:如果二叉树的每个节点的左子树和右子树的深度不大于1,它就是平衡二叉树。
10+
先写一个求深度的函数,再对每一个节点判断,看该节点的左子树的深度和右子树的深度的差是否大于1
11+
12+
31ms
13+
5520k
14+
'''
15+
16+
# -*- coding:utf-8 -*-
17+
# class TreeNode:
18+
# def __init__(self, x):
19+
# self.val = x
20+
# self.left = None
21+
# self.right = None
22+
class Solution:
23+
def IsBalanced_Solution(self, pRoot):
24+
# write code here
25+
if pRoot == None:
26+
# 返回True是因为这是最后的判断依据,在不断递归中,最后是叶子节点,即终止,如果叶子节点时,依然左右子树之差小于1,那么就是平衡二叉树,返回True
27+
return True
28+
depth1 = self.GetDepth(pRoot.left)
29+
depth2 = self.GetDepth(pRoot.right)
30+
if abs(depth1 - depth2) > 1:
31+
return False
32+
return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right)
33+
34+
def GetDepth(self, root):
35+
if not root:
36+
return 0
37+
return max(self.GetDepth(root.left), self.GetDepth(root.right)) + 1

0 commit comments

Comments
 (0)