[Swift]LeetCode1166.设计文件系统 | Design File System
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(www.zengqiang.org)
➤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. ReturnsFalseif the path already exists or its parent path doesn't exist. - get(path): Returns the value associated with a path or returns
-1if 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^4in total. 2 <= path.length <= 1001 <= 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 <= 1001 <= value <= 10^9
2436 ms
1 class FileSystem { 2 var root:Node 3 4 init() { 5 self.root = Node("",-1) 6 } 7 8 func create(_ path: String, _ value: Int) -> Bool { 9 if path.count <= 1 {return false} 10 let split:[String] = path.components(separatedBy:"/") 11 var curr:Node = root 12 for i in 1..<split.count - 1 13 { 14 if curr.contents[split[i]] == nil 15 { 16 return false 17 } 18 curr = curr.contents[split[i]]! 19 } 20 var fileName:String = split.last! 21 curr.contents[fileName] = Node(fileName, value) 22 return true 23 } 24 25 func get(_ path: String) -> Int { 26 let split:[String] = path.components(separatedBy:"/") 27 var curr:Node = root 28 for i in 1..<split.count 29 { 30 if curr.contents[split[i]] == nil 31 { 32 return -1 33 } 34 curr = curr.contents[split[i]]! 35 } 36 return curr.val 37 } 38 } 39 40 class Node 41 { 42 var name:String 43 var contents:[String:Node] 44 var val:Int 45 init(_ name:String,_ val:Int) 46 { 47 self.name = name 48 self.contents = [String:Node]() 49 self.val = val 50 } 51 } 52 53 /** 54 * Your FileSystem object will be instantiated and called as such: 55 * let obj = FileSystem() 56 * let ret_1: Bool = obj.create(path, value) 57 * let ret_2: Int = obj.get(path) 58 */

浙公网安备 33010602011771号