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)
相关推荐
TheJustice_2 分钟前
Linux 常用指令详解
linux·运维·服务器
Tassel_YUE4 分钟前
linux修改内核实现禁止被ping
linux·运维·笔记
fs哆哆7 分钟前
ExcelVBA运用Excel的【条件格式】(二)
linux·运维·服务器·前端·excel
缘友一世15 分钟前
Armbian 1panel面板工具箱中FTP服务无法正常启动的解决方法
linux·运维·后端·1panel
程序猿零零漆18 分钟前
【面向就业的Linux基础】从入门到熟练,探索Linux的秘密(十二)-管道、环境变量、常用命令
linux·运维·服务器
A-刘晨阳20 分钟前
ELFK 8.12.2 部署 -- docker部署方式⚽
linux·运维·elk·docker·容器
little redcap20 分钟前
下载linux的吐槽
linux·运维·服务器
Ddddddd_15825 分钟前
C++ | Leetcode C++题解之第216题组合总和III
c++·leetcode·题解
每天努力进步!27 分钟前
LeetCode热题100刷题8:54. 螺旋矩阵、73. 矩阵置零、48. 旋转图像
c++·算法·leetcode·矩阵
观鉴词recommend35 分钟前
【c++刷题笔记-贪心】day28: 134. 加油站 、 135. 分发糖果 、860.柠檬水找零 、 406.根据身高重建队列
c++·笔记·算法·leetcode