Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 1.15 KB

1166. Design File System.md

File metadata and controls

46 lines (34 loc) · 1.15 KB

1166. Design File System

https://leetcode.com/problems/design-file-system/

solution

class TrieNode:
    def __init__(self, value=0):
        self.children = {}
        self.value = value

class FileSystem:
    def __init__(self):
        self.root = TrieNode()

    def createPath(self, path: str, value: int) -> bool:
        node = self.root
        subpaths = path.split('/')

        for i in range(1, len(subpaths) - 1):
            if subpaths[i] not in node.children:
                return False
            node = node.children[subpaths[i]]

        if subpaths[-1] in node.children:
            return False

        node.children[subpaths[-1]] = TrieNode(value)
        return True

    def get(self, path: str) -> int:
        node = self.root
        for subpath in path.split('/')[1:]:
            if subpath not in node.children:
                return -1
            node = node.children[subpath]
        return node.value

时间复杂度:O()
空间复杂度:O()

follow up

588. Design In-Memory File System