[Swift]LeetCode1166.設計文件系統 | Design File System

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-mwuvpfee-kg.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

You are asked to design a file system which provides two functions:git

  • 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.github

Implement the two functions.微信

Please refer to the examples for clarifications. less

Example 1:ide

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:this

  • 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

你須要設計一個能提供下面兩個函數的文件系統:spa

  • 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  */
相關文章
相關標籤/搜索