Skip to content

Commit a311bb2

Browse files
committed
2019-5-23
1 parent 2dd16af commit a311bb2

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def flatten(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: None Do not return anything, modify root in-place instead.
13+
"""
14+
15+
if not root or (not root.left and not root.right):
16+
return root
17+
18+
#先把左右子树捋直
19+
self.flatten(root.left)
20+
self.flatten(root.right)
21+
22+
tmp = root.right #把捋直的右子树备份一下
23+
24+
root.right = root.left #把捋直的左子树放到右边
25+
root.left = None #记得把左子树置空
26+
while(root.right): #找到现在右子树的最后一个node
27+
root = root.right
28+
root.right = tmp #把捋直的原来的右子树接上去
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
# Definition for a Node.
3+
class Node(object):
4+
def __init__(self, val, left, right, next):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
self.next = next
9+
"""
10+
class Solution(object):
11+
def connect(self, root):
12+
"""
13+
:type root: Node
14+
:rtype: Node
15+
"""
16+
#先排除掉不需要处理的情况
17+
if not root or (not root.left and not root.right):
18+
return root
19+
20+
#某一个节点的左孩子的next一定是指向这个节点的右孩子
21+
root.left.next = root.right
22+
23+
#当某一个节点的next不为空的时候,这个节点的右孩子的next一定是指向该节点next的left
24+
if root.next:
25+
root.right.next = root.next.left
26+
27+
#递归处理下一层
28+
self.connect(root.left)
29+
self.connect(root.right)
30+
31+
return root
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution(object):
2+
def connect(self, root):
3+
"""
4+
:type root: Node
5+
:rtype: Node
6+
"""
7+
if not root or (not root.left and not root.right):
8+
return root
9+
10+
def findCousin(node, parent):
11+
tmp = parent.next
12+
while(tmp):
13+
if tmp.left:
14+
node.next = tmp.left
15+
break
16+
17+
elif tmp.right:
18+
node.next = tmp.right
19+
break
20+
tmp = tmp.next
21+
22+
if root.left and root.right:
23+
root.left.next = root.right
24+
findCousin(root.right, root)
25+
26+
elif root.left:
27+
findCousin(root.left, root)
28+
29+
elif root.right:
30+
findCousin(root.right, root)
31+
32+
# print root.val, root.right.next
33+
self.connect(root.right)
34+
self.connect(root.left)
35+
36+
37+
return root
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def wordBreak(self, s, wordDict):
3+
"""
4+
:type s: str
5+
:type wordDict: List[str]
6+
:rtype: bool
7+
"""
8+
record = [0]#一开始从开头开始找
9+
10+
for j in range(len(s) + 1):
11+
for i in record:#在之前每一种找法的基础上找
12+
if s[i : j] in wordDict: #找到一种可行的分法,说明最远可以拆分到j
13+
record.append(j)
14+
break
15+
# print record
16+
return record[-1] == len(s)
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def reverseWords(self, string):
3+
"""
4+
:type str: List[str]
5+
:rtype: None Do not return anything, modify str in-place instead.
6+
"""
7+
l = len(string)
8+
if not l:
9+
return
10+
11+
def reverse(start, end): #工具函数,功能是将string[start:end + 1]翻转
12+
left, right = start, end
13+
while(left < right):
14+
string[left], string[right] = string[right], string[left]
15+
left += 1
16+
right -= 1
17+
18+
reverse(0, l - 1) #先整个翻转
19+
first_char_idx = 0
20+
for i, x in enumerate(string):
21+
if x == " ":
22+
reverse(first_char_idx, i - 1) #再把每个单词进行翻转
23+
first_char_idx = i + 1
24+
25+
reverse(first_char_idx, l - 1)#把最后一个单词翻转

0 commit comments

Comments
 (0)