其他分享
首页 > 其他分享> > 1233. Remove Sub-Folders from the Filesystem

1233. Remove Sub-Folders from the Filesystem

作者:互联网

Given a list of folders, remove all sub-folders in those folders and return in any order the folders after removing.

If a folder[i] is located within another folder[j], it is called a sub-folder of it.

The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase English letters. For example, /leetcode and /leetcode/problems are valid paths while an empty string and / are not.

给一堆文件列表,删掉所有的子文件夹。给出最后剩下的文件夹路径

这个其实只需要安字典序排序,然后判断i和i+1是不是包含关系就可以了,如果包含,就丢弃i+1,继续往后看,如果不包含,那i和i+2,i+3也不可能是包含关系了,就可以把i+1也加到答案里去,接下来去判断i+1和后面的是否包含,重复这个过程。有个比较需要注意的地方是,如果判断i是i+1的上层目录,除了判断s1 == s2[:len(s1)]以外还要判断s2[len(s1)]是不是'/'。

class Solution(object):
    def removeSubfolders(self, folder):
        """
        :type folder: List[str]
        :rtype: List[str]
        """
        folder = sorted(folder)
        ans = []
        for f in folder:
            if len(ans) == 0 or ans[-1] != f[:len(ans[-1])] or f[len(ans[-1])] != '/':
                ans.append(f)
        return ans

 

标签:Folders,folders,Sub,包含,s1,Filesystem,len,ans,folder
来源: https://www.cnblogs.com/whatyouthink/p/13359868.html