在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You are asked to design a file system which provides two functions:
The format of a path is one or more concatenated strings of the form: 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:
你需要设计一个能提供下面两个函数的文件系统:
“路径” 是由一个或多个符合下述格式的字符串连接起来形成的:在 例如 好了,接下来就请你来实现这两个函数吧!(请参考示例以获得更多信息)
示例 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 因为该路径不存在。 提示:
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 */
|
请发表评论