zoukankan      html  css  js  c++  java
  • 【leetcode】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.

    Example 1:

    Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
    Output: ["/a","/c/d","/c/f"]
    Explanation: Folders "/a/b/" is a subfolder of "/a" and "/c/d/e" is inside of folder "/c/d" in our filesystem.
    

    Example 2:

    Input: folder = ["/a","/a/b/c","/a/b/d"]
    Output: ["/a"]
    Explanation: Folders "/a/b/c" and "/a/b/d/" will be removed because they are subfolders of "/a".
    

    Example 3:

    Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"]
    Output: ["/a/b/c","/a/b/ca","/a/b/d"]

    Constraints:

    • 1 <= folder.length <= 4 * 10^4
    • 2 <= folder[i].length <= 100
    • folder[i] contains only lowercase letters and '/'
    • folder[i] always starts with character '/'
    • Each folder name is unique.

    解题思路:首先建立字典树,并把folder按元素从短到长排好序,然后遍历folder,并与字典树中已有元素做前缀匹配。如果folder[i]存在于字典树中,表示是其他目录的子目录,删除;如果不存在,则把该目录插入到字典树中。

    代码如下:

    class TreeNode(object):
        def __init__(self, x):
            self.val = x
            self.childDir = {}
            self.isDir = False
    
    class Trie(object):
        dic = {}
        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.root = TreeNode(None)
            self.dic = {}
    
        def insert(self,word):
            node = self.root
            for i in word:
                if i not in node.childDir:
                    node.childDir[i] = TreeNode(i)
                node = node.childDir[i]
            node.isDir = True
    
        def isDelete(self,dir):
            node = self.root
            for i in dir:
                if i in node.childDir:
                    node = node.childDir[i]
                    if node.isDir == True:
                        return True
                else:
                    return False
            return False
    
    class Solution(object):
        def removeSubfolders(self, folder):
            """
            :type folder: List[str]
            :rtype: List[str]
            """
            folder.sort(cmp=lambda x1,x2:len(x1) - len(x2))
            trie = Trie()
            res = []
            for f in folder:
                if trie.isDelete(f) == False:
                    res.append(f)
                    trie.insert(f + '/')
            return res
  • 相关阅读:
    poj2192
    poj2002
    poj2190
    poj2001
    poj2195
    自我成长:20岁到40岁的简单人生规划
    最容易让人失恋的十种职业
    职场发展的5W问题(对我影响很大)
    你个人知识管理了吗?
    安慰人的10大原则——当不知该说什么时
  • 原文地址:https://www.cnblogs.com/seyjs/p/11713643.html
Copyright © 2011-2022 走看看