Skip to content

Commit 585ee22

Browse files
author
Jiayang Wu
committed
2019-11-14
1 parent a43ff4d commit 585ee22

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
# Definition for a Node.
3+
class Node(object):
4+
def __init__(self, val, prev, next, child):
5+
self.val = val
6+
self.prev = prev
7+
self.next = next
8+
self.child = child
9+
"""
10+
class Solution(object):
11+
def flatten(self, head):
12+
"""
13+
:type head: Node
14+
:rtype: Node
15+
"""
16+
if not head:
17+
return head
18+
19+
def helper(node):
20+
# 返回的是最后一个节点
21+
if not node:
22+
return
23+
while node:
24+
nxt = node.next # 备份 next
25+
if not nxt:
26+
tail = node # 记录 tail,用于返回
27+
if node.child:
28+
node.next = node.child # 把child 变成next
29+
node.next.prev = node
30+
t = helper(node.child) # 递归处理,t 是处理之后的 原来的child 的最后一个节点
31+
node.child = None # 把child 置空
32+
if nxt: # 如果有next 部分,就让next的prev指向 原来的child 处理之后的最后一个节点
33+
nxt.prev = t
34+
t.next = nxt # 让 t.next 指向原来的 next
35+
node = node.next
36+
return tail
37+
helper(head)
38+
return head
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution(object):
2+
def expressiveWords(self, S, words):
3+
"""
4+
:type S: str
5+
:type words: List[str]
6+
:rtype: int
7+
"""
8+
9+
s = set(S)
10+
res = 0
11+
for word in words:
12+
if len(S) < len(word):
13+
continue
14+
15+
i, j = 0, 0
16+
flag = 0
17+
while i < len(S) and j < len(word):
18+
if S[i] != word[j]:
19+
flag = 1
20+
break
21+
pre = S[i]
22+
cnt_i = 0
23+
while i < len(S) and S[i] == pre:
24+
i += 1
25+
cnt_i += 1
26+
27+
cnt_j = 0
28+
while j < len(word) and word[j] == pre:
29+
j += 1
30+
cnt_j += 1
31+
32+
# print cnt_i, cnt_j
33+
if (cnt_i < 3 and cnt_i != cnt_j) or cnt_i < cnt_j:
34+
flag = 1
35+
36+
if not flag and i == len(S):
37+
res += 1
38+
return res

0 commit comments

Comments
 (0)