• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode1166.设计文件系统|DesignFileSystem

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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. Returns False 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

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  */

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift.左侧弧形tableView,50行代码搞定发布时间:2022-07-13
下一篇:
Swift中的for循环基本使用发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap