leetcode - 1166. Design File System

Description

You are asked to design a file system that allows you to create new paths and associate them with different values.

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.

Implement the FileSystem class:

  • bool createPath(string path, int value) Creates a new path and associates a value to it if possible and returns true. Returns false if the path already exists or its parent path doesn't exist.
  • int get(string path) Returns the value associated with path or returns -1 if the path doesn't exist.

Example 1:

Input: 
["FileSystem","createPath","get"]
[[],["/a",1],["/a"]]
Output: 
[null,true,1]
Explanation: 
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/a", 1); // return true
fileSystem.get("/a"); // return 1

Example 2:

Input: 
["FileSystem","createPath","createPath","get","createPath","get"]
[[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",1],["/c"]]
Output: 
[null,true,true,2,false,-1]
Explanation: 
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/leet", 1); // return true
fileSystem.createPath("/leet/code", 2); // return true
fileSystem.get("/leet/code"); // return 2
fileSystem.createPath("/c/d", 1); // return false because the parent path "/c" doesn't exist.
fileSystem.get("/c"); // return -1 because this path doesn't exist.

Constraints:

2 <= path.length <= 100
1 <= value <= 10^9
Each path is valid and consists of lowercase English letters and '/'.
At most 104 calls in total will be made to createPath and get.

Solution

Trie tree

Code

python3 复制代码
class TrieNode:
    def __init__(self, val: int):
        self.child = {}
        self.val = val

class FileSystem:

    def __init__(self):
        self.root = TrieNode(-1)

    def createPath(self, path: str, value: int) -> bool:
        paths = path.split('/')[1:]
        node = self.root
        for each_path in paths[:-1]:
            if each_path not in node.child:
                return False
            node = node.child[each_path]
        if paths[-1] in node.child:
            return False
        # create the last directory
        node.child[paths[-1]] = TrieNode(value)
        return True
        

    def get(self, path: str) -> int:
        node = self.root
        paths = path.split('/')[1:]
        for each_path in paths:
            if each_path in node.child:
                node = node.child[each_path]
            else:
                return -1
        return node.val


# Your FileSystem object will be instantiated and called as such:
# obj = FileSystem()
# param_1 = obj.createPath(path,value)
# param_2 = obj.get(path)
相关推荐
远游客07132 小时前
centos stream 8下载安装遇到的坑
linux·服务器·centos
马甲是掉不了一点的<.<2 小时前
本地电脑使用命令行上传文件至远程服务器
linux·scp·cmd·远程文件上传
jingyu飞鸟2 小时前
centos-stream9系统安装docker
linux·docker·centos
超爱吃士力架2 小时前
邀请逻辑
java·linux·后端
南宫生2 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
Lenyiin3 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin
LIKEYYLL4 小时前
GNU Octave:特性、使用案例、工具箱、环境与界面
服务器·gnu
云云3214 小时前
搭建云手机平台的技术要求?
服务器·线性代数·安全·智能手机·矩阵
云云3215 小时前
云手机有哪些用途?云手机选择推荐
服务器·线性代数·安全·智能手机·矩阵
cominglately5 小时前
centos单机部署seata
linux·运维·centos