Skip to content

Commit

Permalink
2019-11-14
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiayang Wu committed Nov 15, 2019
1 parent a43ff4d commit 585ee22
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
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
38 changes: 38 additions & 0 deletions 0809.情感丰富的文字/0809-情感丰富的文字.py
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

0 comments on commit 585ee22

Please sign in to comment.