三天都寫一寫 寫得滿手是血 我好爛...姆咪... def dfs(self, head, root): if head is None: return True if root is None: return False if head.val == root.val: return self.dfs(head.next, root.left) or self.dfs(head.next, root.right) else: return False def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool: if root is None: return False elif self.dfs(head,root): return True else: return self.isSubPath(head,root.left) or self.isSubPath(head,root.right) -- def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]: n = 0 cur = head while cur: n += 1 cur = cur.next m = floor(n/k) r = n-m*k ans = [] cur = head for i in range(k): part_head = cur pre = None for _ in range(m+(i<r)): if cur: pre = cur cur = cur.next if pre: pre.next = None ans.append(part_head) return ans -- def spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]: ans = [[-1 for _ in range(n)] for _ in range(m)] cur_i, cur_j, di, dj = 0, 0, 0, 1 while head: ans[cur_i][cur_j] = head.val head = head.next if di!=0 and (cur_i+di<0 or cur_i+di>=m or ans[cur_i+di][cur_j]!=-1): di,dj = 0,-di if dj!=0 and (cur_j+dj<0 or cur_j+dj>=n or ans[cur_i][cur_j+dj]!=-1): dj,di = 0,dj cur_i += di cur_j += dj return ans -- ※ 發信站: 批踢踢實業坊(pttweb.org.tw), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://pttweb.org.tw/Marginalman/M.1725898785.A.E74
JIWP: 大師 09/10 00:20
oin1104: 大師 09/10 00:21
dont: 大師 09/10 01:08