链接:https://leetcode-cn.com/problems/remove-sub-folders-from-the-filesystem/
难度:medium
解题思路:按字母序排列,那么默认就以前缀树的方式排好了,然后只需要遍历,看看是否前缀已经存在
func removeSubfolders(folder []string) []string {
sort.Strings(folder)
res := []string {}
res = append(res, folder[0])
pilot := folder[0]
for i := 1; i < len(folder); {
if strings.HasPrefix(folder[i], pilot + "/") {
i = i + 1
} else {
res = append(res, folder[i])
pilot = folder[i]
i = i + 1
}
}
return res
}
执行用时 : 76 ms , 在所有 Go 提交中击败了 60.71% 的用户
内存消耗 : 10 MB , 在所有 Go 提交中击败了 100.00% 的用户