Skip to content

Commit c23ce0e

Browse files
add some leetcodes
1 parent a45f77f commit c23ce0e

File tree

7 files changed

+317
-145
lines changed

7 files changed

+317
-145
lines changed

.idea/workspace.xml

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

Target Offer/二叉树中和为某一值的路径.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,36 @@ def __init__(self, x):
1010
self.right = None
1111
class Solution:
1212
# 返回二维列表,内部每个列表表示找到的路径
13-
def FindPath(self, root, expectNumber):
14-
if root == None or root.val > expectNumber:
13+
def FindPath(self, root, sum):
14+
if not root:
1515
return []
16-
elif root.val == expectNumber:
17-
if root.left or root.right:
18-
return [] # 因为路径的定义是从根节点到叶节点所经过的结点, 如果是根结点到任意可到达结点的和为待求值, 可以去掉这个判定
16+
if root.left == None and root.right == None:
17+
if sum == root.val:
18+
return [[root.val]]
1919
else:
20-
return [[expectNumber]]
21-
20+
return []
2221
stack = []
23-
if root.left:
24-
stackLeft = self.FindPath(root.left, expectNumber-root.val)
25-
for i in stackLeft:
26-
i.insert(0, root.val)
27-
stack.append(i)
28-
if root.right:
29-
stackRight = self.FindPath(root.right, expectNumber-root.val)
30-
for i in stackRight:
31-
i.insert(0, root.val)
32-
stack.append(i)
22+
leftStack = self.pathSum(root.left, sum - root.val)
23+
for i in leftStack:
24+
i.insert(0, root.val)
25+
stack.append(i)
26+
rightStack = self.pathSum(root.right, sum - root.val)
27+
for i in rightStack:
28+
i.insert(0, root.val)
29+
stack.append(i)
3330
return stack
3431

32+
# 优化写法
33+
def pathSum(self, root, sum):
34+
if not root: return []
35+
if root.left == None and root.right == None:
36+
if sum == root.val:
37+
return [[root.val]]
38+
else:
39+
return []
40+
a = self.pathSum(root.left, sum - root.val) + self.pathSum(root.right, sum - root.val)
41+
return [[root.val] + i for i in a]
42+
3543
pNode1 = TreeNode(10)
3644
pNode2 = TreeNode(5)
3745
pNode3 = TreeNode(12)
@@ -46,4 +54,6 @@ def FindPath(self, root, expectNumber):
4654

4755

4856
S = Solution()
49-
print(S.FindPath(pNode1, 22))
57+
print(S.FindPath(pNode1, 22))
58+
# 测试用例:[1,-2,-3,1,3,-2,null,-1] -1
59+
# 测试用例:[-2, None, -3] -5

leetcode/112. Path Sum.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
3+
4+
For example:
5+
Given the below binary tree and sum = 22,
6+
5
7+
/ \
8+
4 8
9+
/ / \
10+
11 13 4
11+
/ \ \
12+
7 2 1
13+
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
14+
'''
15+
# Definition for a binary tree node.
16+
class TreeNode(object):
17+
def __init__(self, x):
18+
self.val = x
19+
self.left = None
20+
self.right = None
21+
22+
class Solution(object):
23+
def hasPathSum(self, root, sum):
24+
if not root:
25+
return False
26+
if root.val == sum and root.left is None and root.right is None:
27+
return True
28+
return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)

leetcode/113. Path Sum II.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
3+
4+
For example:
5+
Given the below binary tree and sum = 22,
6+
5
7+
/ \
8+
4 8
9+
/ / \
10+
11 13 4
11+
/ \ / \
12+
7 2 5 1
13+
return
14+
[
15+
[5,4,11,2],
16+
[5,8,4,5]
17+
]
18+
'''
19+
20+
21+
# Definition for a binary tree node.
22+
class TreeNode(object):
23+
def __init__(self, x):
24+
self.val = x
25+
self.left = None
26+
self.right = None
27+
28+
class Solution(object):
29+
def pathSum(self, root, sum):
30+
if not root:
31+
return []
32+
if root.left == None and root.right == None:
33+
if sum == root.val:
34+
return [[root.val]]
35+
else:
36+
return []
37+
stack = []
38+
leftStack = self.pathSum(root.left, sum - root.val)
39+
for i in leftStack:
40+
i.insert(0, root.val)
41+
stack.append(i)
42+
rightStack = self.pathSum(root.right, sum - root.val)
43+
for i in rightStack:
44+
i.insert(0, root.val)
45+
stack.append(i)
46+
return stack

leetcode/192.WordFrequency.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
:<<!EOF!
3+
Write a bash script to calculate the frequency of each word in a text file words.txt.
4+
5+
For simplicity sake, you may assume:
6+
7+
words.txt contains only lowercase characters and space ' ' characters.
8+
Each word must consist of lowercase characters only.
9+
Words are separated by one or more whitespace characters.
10+
For example, assume that words.txt has the following content:
11+
12+
the day is sunny the the
13+
the sunny is is
14+
15+
Your script should output the following, sorted by descending frequency:
16+
the 4
17+
is 3
18+
sunny 2
19+
day 1
20+
Note:
21+
!EOF!
22+
23+
awk '{for(i=1;i<=NF;i++) a[$i]++} END {for(k in a) print k,a[k]}' words.txt | sort -k2 -nr
24+
:<<!EOF!
25+
注释:
26+
awk文本分析;NF的值是每行的总列数;--k2 表示按照第二列排序 -nr表示按照逆序排列
27+
!EOF!
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Given a sorted linked list, delete all duplicates such that each element appear only once.
3+
4+
For example,
5+
Given 1->1->2, return 1->2.
6+
Given 1->1->2->3->3, return 1->2->3.
7+
'''
8+
9+
# Definition for singly-linked list.
10+
class ListNode(object):
11+
def __init__(self, x):
12+
self.val = x
13+
self.next = None
14+
15+
class Solution(object):
16+
def deleteDuplicates(self, head):
17+
"""
18+
:type head: ListNode
19+
:rtype: ListNode
20+
"""
21+
pNode = head
22+
while pNode:
23+
while pNode.next and pNode.next.val == pNode.val:
24+
pNode.next = pNode.next.next
25+
pNode = pNode.next
26+
return head

leetcode/words.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
the day is sunny
2+
the the the sunny is is

0 commit comments

Comments
 (0)