Re: [閒聊] 每日leetcode

看板Marginalman作者 (通通打死)時間1年前 (2024/09/10 00:19), 編輯推噓2(201)
留言3則, 3人參與, 1年前最新討論串832/1548 (看更多)
三天都寫一寫 寫得滿手是血 我好爛...姆咪... 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 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725898785.A.E74.html

09/10 00:20, 1年前 , 1F
大師
09/10 00:20, 1F

09/10 00:21, 1年前 , 2F
大師
09/10 00:21, 2F

09/10 01:08, 1年前 , 3F
大師
09/10 01:08, 3F
文章代碼(AID): #1cto0Xvq (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cto0Xvq (Marginalman)