作者DJYOMIYAHINA (通通打死)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Fri Oct 25 23:27:47 2024
很好
你很腦殘嗎
敢這樣用sort
我死也不會放過你
我給你一個選擇
1. 我把你送去喝oin的水水
要是敢在這邊用sort
你就別怪我不客氣了
def removeSubfolders(self, folder: List[str]) -> List[str]:
class Node:
def __init__(self):
self.child = {}
self.isFolder = False
root = Node()
# build Trie
for path in folder:
folders = path.split('/')[1:]
cur = root
for f in folders:
if f in cur.child:
cur = cur.child[f]
else:
cur.child[f] = Node()
cur = cur.child[f]
cur.isFolder = True
# inference
ans = []
def dfs(cur_path, root):
if root.isFolder == True:
ans.append(cur_path)
return
for folder, node in root.child.items():
next_path = cur_path + f"/{folder}"
dfs(next_path, node)
dfs("", root)
return ans
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1729870069.A.3A8.html