-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jiayang Wu
committed
Nov 15, 2019
1 parent
a43ff4d
commit 585ee22
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
# Definition for a Node. | ||
class Node(object): | ||
def __init__(self, val, prev, next, child): | ||
self.val = val | ||
self.prev = prev | ||
self.next = next | ||
self.child = child | ||
""" | ||
class Solution(object): | ||
def flatten(self, head): | ||
""" | ||
:type head: Node | ||
:rtype: Node | ||
""" | ||
if not head: | ||
return head | ||
|
||
def helper(node): | ||
# 返回的是最后一个节点 | ||
if not node: | ||
return | ||
while node: | ||
nxt = node.next # 备份 next | ||
if not nxt: | ||
tail = node # 记录 tail,用于返回 | ||
if node.child: | ||
node.next = node.child # 把child 变成next | ||
node.next.prev = node | ||
t = helper(node.child) # 递归处理,t 是处理之后的 原来的child 的最后一个节点 | ||
node.child = None # 把child 置空 | ||
if nxt: # 如果有next 部分,就让next的prev指向 原来的child 处理之后的最后一个节点 | ||
nxt.prev = t | ||
t.next = nxt # 让 t.next 指向原来的 next | ||
node = node.next | ||
return tail | ||
helper(head) | ||
return head |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Solution(object): | ||
def expressiveWords(self, S, words): | ||
""" | ||
:type S: str | ||
:type words: List[str] | ||
:rtype: int | ||
""" | ||
|
||
s = set(S) | ||
res = 0 | ||
for word in words: | ||
if len(S) < len(word): | ||
continue | ||
|
||
i, j = 0, 0 | ||
flag = 0 | ||
while i < len(S) and j < len(word): | ||
if S[i] != word[j]: | ||
flag = 1 | ||
break | ||
pre = S[i] | ||
cnt_i = 0 | ||
while i < len(S) and S[i] == pre: | ||
i += 1 | ||
cnt_i += 1 | ||
|
||
cnt_j = 0 | ||
while j < len(word) and word[j] == pre: | ||
j += 1 | ||
cnt_j += 1 | ||
|
||
# print cnt_i, cnt_j | ||
if (cnt_i < 3 and cnt_i != cnt_j) or cnt_i < cnt_j: | ||
flag = 1 | ||
|
||
if not flag and i == len(S): | ||
res += 1 | ||
return res |