LC-588 Design In-Memory File System

Build the file system like a multi-branched tree, each tree node is either a directory or a file.

![Uploading image_760038.png . . .]

        (False, "", {a, a2})  # home dir 
                    / \
                   /   \
   { (False, "", {}),  (False, "", {b}) }   #a, a2 dir 
                                     |
                      { (False, "", {c, file_in_b}) }      #b dir 
                                     /\
                                    /  \
                     { (False, "", {}), (True, "content", {}) }  # c dir, file_in_b

class Node:
    def __init__(self):
        self.is_file = False   
        self.file_content = "" 
        self.children = {}     # key is dir or file name

class FileSystem:
    def __init__(self):
        self.fs = Node()

    def ls(self, path):
        """
        :type path: str
        :rtype: List[str]
        """
        node = self.fs
        # omit empty string
        dirs = [dir for dir in path.split('/') if dir.strip() != '']
        ans = []
        # if path = '/', this for loop will be skipped
        for dir in dirs:
            if dir not in node.children:
                return ans
            node = node.children[dir]

        if node.is_file:
            ans.append(dirs[-1])
        else:
            for k, _ in node.children.items():
                ans.append(k)
        return sorted(ans)

    def mkdir(self, path):
        """
        :type path: str
        :rtype: void
        """
        node = self.fs
        dirs = [dir for dir in path.split('/') if dir.strip() != '']
        for dir in dirs:
            if dir not in node.children:
                node.children[dir] = Node()
            node = node.children[dir]


    def addContentToFile(self, filePath, content):
        """
        :type filePath: str
        :type content: str
        :rtype: void
        """
        node = self.fs
        dirs = [dir for dir in filePath.split('/') if dir.strip() != '']
        for dir in dirs:
            if dir not in node.children:
                node.children[dir] = Node()
            node = node.children[dir]
        # mark it as a file
        node.is_file = True
        # append content to it, default content is empty string
        node.file_content += content

    def readContentFromFile(self, filePath):
        """
        :type filePath: str
        :rtype: str
        """
        node = self.fs
        dirs = [dir for dir in filePath.split('/') if dir.strip() != '']
        for dir in dirs:
            if dir not in node.children:
                node.children[dir] = Node()
            node = node.children[dir]
        return node.file_content

        # Your FileSystem object will be instantiated and called as such:
        # obj = FileSystem()
        # param_1 = obj.ls(path)
        # obj.mkdir(path)
        # obj.addContentToFile(filePath,content)
        # param_4 = obj.readContentFromFile(filePath)

你可能感兴趣的:(LC-588 Design In-Memory File System)