topic:
1233. Delete the subfolder folder
Thought:
脑子里的构思yesDictionary tree,ButClanguage
的Dictionary tree,Whether it isPython
still golang
I won’t write。 So helpless can only look at the answer。
ylbThe big guy is right firstlistSort,Then traverse the list。
1.ifiBefore[len(ans[-1])]
Just happensans[-1]
The element is consistent,Explainiyes
ans[-1]Subfolder folder,Then skip。
2.ifyes["/a/b/c","/a/b/ca","/a/b/d"]
This situation,Be alone1.的判断yes不够的,at this time"/a/b/ca"
Before缀and"/a/b/c"
yes一样的,但yescaandc实际上yes两个不同的文件夹,So add a judgment front end
yes否yes/
,"/a/b/ca"
and"/a/b/c/a"
中后者才会被判定yes前面一个元素的子集。
Dictionary tree没太看懂:
我们可以使用Dictionary tree存储数组 folder All folders in。
Dictionary tree的每个节点包含 children Field,Sub -nodes for storing the current nodes,as well as fid Field,
Used to store the folder corresponding to the current node in the array folder Bidding。
For array folder Each folder in it f,We will first f according to / Divide into several skewers,
Then start from the root node,依次将子串加入Dictionary tree中。Next,我们从根节点开始搜索Dictionary tree,
if当前节点的 fid Field不为 -1,则说明当前节点对应的文件夹yes答案数组中的一个文件夹,
We add it to the array and return。otherwise,We search for all sub -nodes of the current node,Finally return to the array of answers。
Code:
1 | class Solution: |
1 | func removeSubfolders(folder []string) (ans []string) { |