★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:为敢(WeiGanTechnologies)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11407049.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
You are asked to design a file system which provides two functions:
- create(path, value): Creates a new path and associates a value to it if possible and returns
True
. ReturnsFalse
if the path already exists or its parent path doesn't exist. - get(path): Returns the value associated with a path or returns
-1
if the path doesn't exist.
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 two functions.
Please refer to the examples for clarifications.
Example 1:
Input: ["FileSystem","create","get"] [[],["/a",1],["/a"]] Output: [null,true,1] Explanation: FileSystem fileSystem = new FileSystem(); fileSystem.create("/a", 1); // return true fileSystem.get("/a"); // return 1
Example 2:
Input: ["FileSystem","create","create","get","create","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.create("/leet", 1); // return true fileSystem.create("/leet/code", 2); // return true fileSystem.get("/leet/code"); // return 2 fileSystem.create("/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:
- The number of calls to the two functions is less than or equal to
10^4
in total. 2 <= path.length <= 100
1 <= value <= 10^9
你需要设计一个能提供下面两个函数的文件系统:
- create(path, value): 创建一个新的路径,并尽可能将值
value
与路径path
关联,然后返回True
。如果路径已经存在或者路径的父路径不存在,则返回False
。 - get(path): 返回与路径关联的值。如果路径不存在,则返回
-1
。
“路径” 是由一个或多个符合下述格式的字符串连接起来形成的:在 /
后跟着一个或多个小写英文字母。
例如 /leetcode
和 /leetcode/problems
都是有效的路径,但空字符串和 /
不是有效的路径。
好了,接下来就请你来实现这两个函数吧!(请参考示例以获得更多信息)
示例 1:
输入: ["FileSystem","create","get"] [[],["/a",1],["/a"]] 输出: [null,true,1] 解释: FileSystem fileSystem = new FileSystem(); fileSystem.create("/a", 1); // 返回 true fileSystem.get("/a"); // 返回 1
示例 2:
输入: ["FileSystem","create","create","get","create","get"] [[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",1],["/c"]] 输出: [null,true,true,2,false,-1] 解释: FileSystem fileSystem = new FileSystem(); fileSystem.create("/leet", 1); // 返回 true fileSystem.create("/leet/code", 2); // 返回 true fileSystem.get("/leet/code"); // 返回 2 fileSystem.create("/c/d", 1); // 返回 false 因为父路径 "/c" 不存在。 fileSystem.get("/c"); // 返回 -1 因为该路径不存在。
提示:
- 对两个函数的调用次数加起来小于等于
10^4
2 <= path.length <= 100
1 <= value <= 10^9