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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 当我发现他的情绪能深深的影响到我的时候,我意识到了自己的感情 。 并不是我不喜欢英语,不想做老师,而是深刻的意识到...
    陌上墨言阅读 494评论 4 0
  • 如果思念爬上了树梢, 大概枝头会狠狠的弯下, 我目光所及之处全都没有你, 仿佛却在耳畔听到了, 你轻轻唤我的声音,...
    海荼阅读 228评论 0 0
  • 聪明的程序员懂得"偷懒"来提升开发效率.我们在日常开发中,有一些代码片段会经常利用到,大家可能会觉得定义一些宏可以...
    Bryan5137阅读 696评论 0 7
  • 今天晨读分享的书叫《一分钟视力革命》。 文章看完,我放下手机,摘掉眼镜,忍不住开始轻揉眉心。 “主人,我能帮你什么...
    人间最美是清欢阅读 252评论 5 8
  • 始料未及的艰辛,只想过安逸的日子
    Sarah丹阅读 285评论 0 0

友情链接更多精彩内容