Re: [閒聊] 每日leetcode

看板Marginalman作者 (franchouchouISBEST)時間1年前 (2024/09/25 22:04), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串908/1554 (看更多)
我照著昨天的寫 然後又跟昨天一樣慢 不過有過就好 == class Node: def __init__(self): self.child = [None for _ in range(26)] self.cnt = 0 class Solution: def sumPrefixScores(self, words: List[str]) -> List[int]: root = Node() for w in words: cur = root for c in w: if cur.child[ord(c)-ord('a')] is None: cur.child[ord(c)-ord('a')] = Node() cur = cur.child[ord(c)-ord('a')] cur.cnt += 1 ans = [] for w in words: cur = root score = 0 for c in w: score += cur.child[ord(c)-ord('a')].cnt cur = cur.child[ord(c)-ord('a')] ans.append(score) return ans -- https://i.imgur.com/QaQrl0t.jpeg
https://i.imgur.com/yXpuYNA.jpeg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1727273074.A.888.html
文章代碼(AID): #1cz1XoY8 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cz1XoY8 (Marginalman)