Skip to content

Commit f0e9e34

Browse files
committedAug 12, 2016
add
1 parent bff29ea commit f0e9e34

6 files changed

+239
-123
lines changed
 

‎.idea/workspace.xml

+88-83
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎MapReduce_and_filter.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding:UTF-8 -*-
2+
import functools
3+
# Python3.x和Python2.x对于map、reduce、filter的处理变得不同
4+
# Python3.x中map和filter的输出是一个map型和filter型, 需要从里面取出需要的值
5+
# Python2.x中map和filter输出的直接是一个list
6+
# Python3.x中使用reduce需要引入functools
7+
8+
# 使用map把list中的int变为str
9+
map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])
10+
# 使用map()把名字规范化, 首字母大写,其余小写
11+
def standardName(s):
12+
return s.capitalize()
13+
print([x for x in map(standardName, ['adam', 'LISA', 'barT'])])
14+
# 在Python2.x中应该使用print(map(standardName, ['adam', 'LISA', 'barT']))
15+
16+
# 使用reduce()输出一个list的所有数的乘积
17+
def prod(aList):
18+
return functools.reduce(lambda x, y: x*y, aList)
19+
print(prod([1, 2, 3, 4, 5]))
20+
21+
# 使用filter()打印100以内的素数
22+
def isPrime(n):
23+
isPrimeFlag = True
24+
if n <= 1:
25+
isPrimeFlag = False
26+
i = 2
27+
28+
while i * i <= n:
29+
if n % i == 0:
30+
isPrimeFlag = False
31+
break
32+
i += 1
33+
return n if isPrimeFlag else None
34+
print(filter(isPrime, range(101)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
查找二叉搜索树两个结点的最低公共祖先
3+
'''
4+
5+
# -*- coding:utf-8 -*-
6+
class TreeNode:
7+
def __init__(self, x):
8+
self.val = x
9+
self.left = None
10+
self.right = None
11+
class Solution:
12+
def findParent(self, pNode1, pNode2, root):
13+
if pNode1 == None or pNode2 == None:
14+
return
15+
if pNode1 == pNode2:
16+
return
17+
val1 = pNode1.val
18+
val2 = pNode2.val
19+
while root != None:
20+
if (val1 - root.val) * (val2 - root.val) <= 0:
21+
return root.val
22+
elif val1 > root.val and val2 > root.val:
23+
root = root.right
24+
else:
25+
root = root.left
26+
27+
return False
28+
29+
pNode1 = TreeNode(8)
30+
pNode2 = TreeNode(6)
31+
pNode3 = TreeNode(10)
32+
pNode4 = TreeNode(5)
33+
pNode5 = TreeNode(7)
34+
pNode6 = TreeNode(9)
35+
pNode7 = TreeNode(11)
36+
37+
pNode1.left = pNode2
38+
pNode1.right = pNode3
39+
pNode2.left = pNode4
40+
pNode2.right = pNode5
41+
pNode3.left = pNode6
42+
pNode3.right = pNode7
43+
44+
S = Solution()
45+
print(S.findParent(pNode3, pNode7, pNode1))

‎Target Offer/二叉树的最低公共祖先.py

+49-40
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,54 @@
1-
'''
2-
查找二叉搜索树两个结点的最低公共祖先
3-
'''
4-
5-
# -*- coding:utf-8 -*-
1+
# 二叉树的最低公共祖先
2+
"""
3+
Definition of TreeNode:
4+
"""
65
class TreeNode:
7-
def __init__(self, x):
8-
self.val = x
9-
self.left = None
10-
self.right = None
6+
def __init__(self, val):
7+
self.val = val
8+
self.left, self.right = None, None
119
class Solution:
12-
def findParent(self, pNode1, pNode2, root):
13-
if pNode1 == None or pNode2 == None:
14-
return
15-
if pNode1 == pNode2:
16-
return
17-
val1 = pNode1.val
18-
val2 = pNode2.val
19-
while root != None:
20-
if (val1 - root.val) * (val2 - root.val) <= 0:
21-
return root.val
22-
elif val1 > root.val and val2 > root.val:
23-
root = root.right
24-
else:
25-
root = root.left
10+
"""
11+
@param root: The root of the binary search tree.
12+
@param A and B: two nodes in a Binary.
13+
@return: Return the least common ancestor(LCA) of the two nodes.
14+
"""
2615

27-
return False
28-
29-
pNode1 = TreeNode(8)
30-
pNode2 = TreeNode(6)
31-
pNode3 = TreeNode(10)
32-
pNode4 = TreeNode(5)
33-
pNode5 = TreeNode(7)
34-
pNode6 = TreeNode(9)
35-
pNode7 = TreeNode(11)
16+
def lowestCommonAncestor(self, root, A, B):
17+
# write your code here
18+
if root == None:
19+
return False
20+
pathA = self.storeNodes(root, A)[0]
21+
pathB = self.storeNodes(root, B)[0]
22+
if pathA and pathB:
23+
lenA, lenB = len(pathA), len(pathB)
24+
diff = abs(lenA - lenB)
25+
if lenA > lenB:
26+
markA = lenA - diff - 1
27+
markB = lenB - 1
28+
else:
29+
markA = lenA - 1
30+
markB = lenB - diff - 1
31+
while markA >= 0 and markB >= 0:
32+
if pathA[markA] == pathB[markB]:
33+
return pathA[markA]
34+
markA -= 1
35+
markB -= 1
3636

37-
pNode1.left = pNode2
38-
pNode1.right = pNode3
39-
pNode2.left = pNode4
40-
pNode2.right = pNode5
41-
pNode3.left = pNode6
42-
pNode3.right = pNode7
37+
def storeNodes(self, root, targetNode):
38+
if root == None or targetNode == None:
39+
return []
40+
elif root.val == targetNode.val:
41+
return [[targetNode]]
42+
stack = []
43+
if root.left:
44+
stackLeft = self.storeNodes(root.left, targetNode)
45+
for i in stackLeft:
46+
i.insert(0, root)
47+
stack.append(i)
48+
if root.right:
49+
stackRight = self.storeNodes(root.right, targetNode)
50+
for i in stackRight:
51+
i.insert(0, root)
52+
stack.append(i)
53+
return stack
4354

44-
S = Solution()
45-
print(S.findParent(pNode3, pNode7, pNode1))

‎Target Offer/二维数组查找.py

+22
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ def Find(self, array, target):
4545
else:
4646
return True
4747
return False
48+
# 扩展, 输出数组中target的个数
49+
def searchMatrix(self, matrix, target):
50+
if matrix == None or len(matrix) == 0:
51+
return 0
52+
rows = len(matrix)
53+
cols = len(matrix[0])
54+
row, col = 0, cols - 1
55+
count = 0
56+
while row <= rows - 1 and col >= 0:
57+
if matrix[row][col] > target:
58+
col -= 1
59+
elif matrix[row][col] < target:
60+
row += 1
61+
else:
62+
count += 1
63+
col -= 1
64+
return count
65+
4866

4967
array = [[1, 2, 8, 9],
5068
[2, 4, 9, 12],
@@ -53,10 +71,14 @@ def Find(self, array, target):
5371
array2 = []
5472
array3 = [['a', 'b', 'c'],
5573
['b', 'c', 'd']]
74+
array4 = [[62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80],[63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81],[64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82],[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83],[66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84],[67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85]]
75+
76+
5677
findtarget = Solution()
5778
print(findtarget.Find(array, 10))
5879
print(findtarget.Find(array, 30))
5980
print(findtarget.Find(array, 13.0))
6081
print(findtarget.Find(array, ''))
6182
print(findtarget.Find(array2, 10))
6283
print(findtarget.Find(array3, 'b'))
84+
print(findtarget.searchMatrix(array4, 81))

‎Target Offer/求前n项和.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def sum0(self, n):
1515
# 利用非0值作两次非运算返回false, 0作两次非运算返回True
1616
def sumN(self, n):
1717
fun = {False: self.sum0, True: self.sumN}
18+
# 此处的fun[not not n] 不能写作func[not not n-1], 否则测试用例为0的话, 就会无限次迭代
1819
return n + fun[not not n](n - 1)
1920

2021
def Sum_Solution2(self, n):

0 commit comments

Comments
 (0)