leetcode 刷题 138 139
【摘要】
"""
# Definition for a Node.
class Node: def __init__(self, val, next, random): self.val = val self.next = next self.random = random
"""
class Solution: def copyRandomList(self, head:...
"""
# Definition for a Node.
class Node: def __init__(self, val, next, random): self.val = val self.next = next self.random = random
"""
class Solution: def copyRandomList(self, head: 'Node') -> 'Node': """ :type head: Node :rtype: Node """ if not head: return head d = {} p = h = Node(0,None,None) q = head while q: p.next = Node(q.val,None,None) p = p.next p.next = None d[q] = p q = q.next p,q = h.next,head while q: if q.random: p.random = d[q.random] p,q = p.next,q.next
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
"""
# Definition for a Node.
class Node: def __init__(self, val, next, random): self.val = val self.next = next self.random = random
"""
class Solution(object): def copyRandomList(self, head): """ :type head: Node :rtype: Node """ if not head: return head cur = head while cur: node = Node(cur.val,None,None) node.next = cur.next cur.next = node cur = node.next cur = head while cur: if cur.random: cur.next.random = cur.random.next cur = cur.next.next cur = head res = head.next while cur: tmp = cur.next cur.next = tmp.next cur = cur.next if cur: tmp.next = cur.next return res
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: starts = [0] for i in range(len(s)): for j in starts: if s[j:i+1] in wordDict: starts.append(i+1) break return starts[-1] == len(s)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: result, max_len = [0], 0 for each in wordDict: if len(each) > max_len: max_len = len(each) for i in range(1, len(s)+1): for j in result: if i-j <= max_len and s[j:i] in wordDict: result.append(i) break return result[-1] == len(s)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
文章来源: maoli.blog.csdn.net,作者:刘润森!,版权归原作者所有,如需转载,请联系作者。
原文链接:maoli.blog.csdn.net/article/details/90745201
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)