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)
相关推荐
Wireless_wifi61 分钟前
Why IPQ5018 Continues to Thrive in the Wi-Fi 7 Era
linux·5g
das2m4 分钟前
Arch Linux (WSL2) Docker 环境踩坑记
linux·docker·eureka
snow@li8 分钟前
nginx:详解与速查表 / Nginx = 反向代理 + 负载均衡 + 静态服务器 + HTTP 缓存 / 请求分发、静态加速、上线不中断
linux·服务器·nginx
开源Z14 分钟前
LeetCode 238 · 除自身以外数组的乘积:左右两遍扫描,不用除法
算法·leetcode
小则又沐风a17 分钟前
进程最终篇---进程控制(模拟实现xshell)
java·linux·服务器·前端
云服务器代理商18 分钟前
阿里云国内版迁移到国际版完整操作教程
服务器·阿里云·云计算·阿里云服务器·阿里云国际·阿里云海外
_codemonster24 分钟前
K8s / K3s 通用 Kubectl 命令大全(表格版)
linux·docker·kubernetes
阿旭超级学得完26 分钟前
Linux基础指令 四(apt,vim,git,cgdb)
linux·服务器·开发语言·数据结构·c++·git·vim
半夜修仙29 分钟前
4.RabbitMQ运维
linux·运维·服务器·分布式·rabbitmq·java-rabbitmq
酉鬼女又兒29 分钟前
零基础入门计算机网络:集线器与交换机区别、以太网交换机自学习转发流程及生成树协议STP全解析
服务器·网络·网络协议·tcp/ip·计算机网络·考研·职场和发展