Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc23ce0e

Browse files
add some leetcodes
1 parenta45f77f commitc23ce0e

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 aboutcustomizing 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
classSolution:
1212
# 返回二维列表,内部每个列表表示找到的路径
13-
defFindPath(self,root,expectNumber):
14-
ifroot==Noneorroot.val>expectNumber:
13+
defFindPath(self,root,sum):
14+
ifnotroot:
1515
return []
16-
elifroot.val==expectNumber:
17-
ifroot.leftorroot.right:
18-
return []# 因为路径的定义是从根节点到叶节点所经过的结点, 如果是根结点到任意可到达结点的和为待求值, 可以去掉这个判定
16+
ifroot.left==Noneandroot.right==None:
17+
ifsum==root.val:
18+
return [[root.val]]
1919
else:
20-
return [[expectNumber]]
21-
20+
return []
2221
stack= []
23-
ifroot.left:
24-
stackLeft=self.FindPath(root.left,expectNumber-root.val)
25-
foriinstackLeft:
26-
i.insert(0,root.val)
27-
stack.append(i)
28-
ifroot.right:
29-
stackRight=self.FindPath(root.right,expectNumber-root.val)
30-
foriinstackRight:
31-
i.insert(0,root.val)
32-
stack.append(i)
22+
leftStack=self.pathSum(root.left,sum-root.val)
23+
foriinleftStack:
24+
i.insert(0,root.val)
25+
stack.append(i)
26+
rightStack=self.pathSum(root.right,sum-root.val)
27+
foriinrightStack:
28+
i.insert(0,root.val)
29+
stack.append(i)
3330
returnstack
3431

32+
# 优化写法
33+
defpathSum(self,root,sum):
34+
ifnotroot:return []
35+
ifroot.left==Noneandroot.right==None:
36+
ifsum==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]+iforiina]
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+
classTreeNode(object):
17+
def__init__(self,x):
18+
self.val=x
19+
self.left=None
20+
self.right=None
21+
22+
classSolution(object):
23+
defhasPathSum(self,root,sum):
24+
ifnotroot:
25+
returnFalse
26+
ifroot.val==sumandroot.leftisNoneandroot.rightisNone:
27+
returnTrue
28+
returnself.hasPathSum(root.left,sum-root.val)orself.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+
classTreeNode(object):
23+
def__init__(self,x):
24+
self.val=x
25+
self.left=None
26+
self.right=None
27+
28+
classSolution(object):
29+
defpathSum(self,root,sum):
30+
ifnotroot:
31+
return []
32+
ifroot.left==Noneandroot.right==None:
33+
ifsum==root.val:
34+
return [[root.val]]
35+
else:
36+
return []
37+
stack= []
38+
leftStack=self.pathSum(root.left,sum-root.val)
39+
foriinleftStack:
40+
i.insert(0,root.val)
41+
stack.append(i)
42+
rightStack=self.pathSum(root.right,sum-root.val)
43+
foriinrightStack:
44+
i.insert(0,root.val)
45+
stack.append(i)
46+
returnstack

‎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+
classListNode(object):
11+
def__init__(self,x):
12+
self.val=x
13+
self.next=None
14+
15+
classSolution(object):
16+
defdeleteDuplicates(self,head):
17+
"""
18+
:type head: ListNode
19+
:rtype: ListNode
20+
"""
21+
pNode=head
22+
whilepNode:
23+
whilepNode.nextandpNode.next.val==pNode.val:
24+
pNode.next=pNode.next.next
25+
pNode=pNode.next
26+
returnhead

‎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)

[8]ページ先頭

©2009-2025 Movatter.jp