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)
相关推荐
开开心心就好18 分钟前
发票信息录入太麻烦?离线工具一键转表格
运维·服务器·前端·pdf·ocr·maven·intellij-idea
运维小斌26 分钟前
麒麟 V10 部署 OceanBase:故障速查与验收清单
linux·数据库·oceanbase·麒麟
水饺编程35 分钟前
第2章:Linux 基础,在 root 中创建用户账号
linux·c语言·汇编·1024程序员节
ITxiaobing202336 分钟前
IP 定位服务怎么选:没有“公认第一”,只有场景匹配
服务器·网络协议·tcp/ip
山东孙燕姿38 分钟前
维修工单的转派与协同:两个人一起上
大数据·运维·服务器·网络·人工智能
LMT流氓兔38 分钟前
第五单元——第9课:防止同一次操作重复执行
java·服务器·数据库·算法·ai·langchain
春弦_44 分钟前
LeetCode Hot 100 刷题笔记
笔记·leetcode·哈希算法
叼烟扛炮1 小时前
Linux 学习笔记(四):文件检索与管道重定向
linux·笔记·学习
玩AI的奶茶1 小时前
GPU 算力租赁平台横向对比:六家主流平台,24GB 档价差 85%
服务器·人工智能·gpu算力·token·算力租赁
帷幕落秋2 小时前
Linux 网络基础
linux