Skip to content

Commit cda8a06

Browse files
modify codes
1 parent 8336aec commit cda8a06

File tree

4 files changed

+67
-79
lines changed

4 files changed

+67
-79
lines changed

.idea/workspace.xml

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

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ def __init__(self, x):
1111
class Solution:
1212
# 返回二维列表,内部每个列表表示找到的路径
1313
def FindPath(self, root, expectNumber):
14-
if root == None:
15-
return []
16-
if root.val > expectNumber:
14+
if root == None or root.val > expectNumber:
1715
return []
1816
elif root.val == expectNumber:
1917
if root.left or root.right:
@@ -24,21 +22,15 @@ def FindPath(self, root, expectNumber):
2422
stack = []
2523
if root.left:
2624
stackLeft = self.FindPath(root.left, expectNumber-root.val)
27-
if stackLeft:
28-
for i in stackLeft:
29-
i.insert(0, root.val)
30-
stack.append(i)
25+
for i in stackLeft:
26+
i.insert(0, root.val)
27+
stack.append(i)
3128
if root.right:
3229
stackRight = self.FindPath(root.right, expectNumber-root.val)
33-
if stackRight:
34-
for i in stackRight:
35-
i.insert(0, root.val)
36-
stack.append(i)
37-
38-
if stack:
39-
return stack
40-
else:
41-
return []
30+
for i in stackRight:
31+
i.insert(0, root.val)
32+
stack.append(i)
33+
return stack
4234

4335
pNode1 = TreeNode(10)
4436
pNode2 = TreeNode(5)

Target Offer/复杂链表的复制.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,32 @@ def Clone(self, pHead):
2020
# 复制原始链表的每个结点, 将复制的结点链接在其原始结点的后面
2121
def CloneNodes(self, pHead):
2222
pNode = pHead
23-
while pNode != None:
23+
while pNode:
2424
pCloned = RandomListNode(0)
2525
pCloned.label = pNode.label
2626
pCloned.next = pNode.next
27-
pCloned.random = None
27+
# pCloned.random = None #不需要写这句话, 因为创建新的结点的时候,random自动指向None
2828

2929
pNode.next = pCloned
30-
3130
pNode = pCloned.next
31+
3232
# 将复制后的链表中的复制结点的random指针链接到被复制结点random指针的后一个结点
3333
def ConnectRandomNodes(self, pHead):
3434
pNode = pHead
35-
while pNode != None:
35+
while pNode:
3636
pCloned = pNode.next
3737
if pNode.random != None:
3838
pCloned.random = pNode.random.next
3939
pNode = pCloned.next
40+
4041
# 拆分链表, 将原始链表的结点组成新的链表, 复制结点组成复制后的链表
4142
def ReconnectNodes(self, pHead):
4243
pNode = pHead
43-
pClonedHead = None
44-
pClonedNode = None
45-
46-
if pNode != None:
47-
pClonedHead = pClonedNode = pNode.next
48-
pNode.next = pClonedHead.next
49-
pNode = pNode.next
44+
pClonedHead = pClonedNode = pNode.next
45+
pNode.next = pClonedHead.next
46+
pNode = pNode.next
5047

51-
while pNode != None:
48+
while pNode:
5249
pClonedNode.next = pNode.next
5350
pClonedNode = pClonedNode.next
5451
pNode.next = pClonedNode.next

0 commit comments

Comments
 (0)