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)
相关推荐
xiaoyaoyou.xyz6 分钟前
嵌入式Linux Qt开发:1、搭建基于ubuntu18.04的Qt开发环境及测试(解决Qt creator输入法问题)
linux·qt
Toky Zhu10 分钟前
ubuntu清除缓存
linux·ubuntu·缓存
影龙帝皖34 分钟前
Linux服务之lvs集群与dr模式部署
linux·服务器·lvs
2302_7995257444 分钟前
【Linux】第十六章 分析和存储日志
linux·运维·服务器
愚润求学1 小时前
【Linux】Ext系列文件系统
linux·运维·服务器·笔记
小阳睡不醒1 小时前
vim 练习题
linux·编辑器·vim
胖大和尚2 小时前
完整的 CentOS 6.10 虚拟机安装启动脚本
linux·运维·centos
小羊在奋斗2 小时前
【LeetCode 热题 100】反转链表 / 回文链表 / 有序链表转换二叉搜索树 / LRU 缓存
算法·leetcode·链表
爱上彩虹c2 小时前
LeetCode Hot100 (1/100)
算法·leetcode·职场和发展
熙曦Sakura2 小时前
【Linux网络】TCP全连接队列
linux·网络·tcp/ip