. [Swift] LeetCode1166 file system design | Design File System

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: to dare (WeiGanTechnologies)
➤ blog Park address: San-ching Wing Chi ( https://www.cnblogs.com/strengthen/ )
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: HTTPS: //www.cnblogs. com / strengthen / p / 11407049.html 
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

You need to design a can provide the following two functions of the file system:

  • create (path, value): create a new path, and value as far as possible  value to the path  path associated with, and then return  True. If the path already exists or parent path path does not exist is returned  False.
  • get (path): returns the value associated with a path. If the path does not exist, it returns  -1.

"Path" is a string of one or more of the following formats conform connected together to form: In the  / rear followed by one or more lowercase letters.

For example,  /leetcode and  /leetcode/problems they are valid path, but the empty string and  / not a valid path.

Well, then please you to achieve these two functions it! (Refer to the sample for more information)

 

Example 1:

输入: 
["FileSystem","create","get"]
[[],["/a",1],["/a"]]
输出: 
[null,true,1]
解释: 
FileSystem fileSystem = new FileSystem();

fileSystem.create("/a", 1); // 返回 true
fileSystem.get("/a"); // 返回 1

Example 2:

Input: 
[ "the FileSystem", "Create", "Create", "GET", "Create", "GET"] 
[[], [ "/ Leet",. 1], [ "/ Leet / code", 2] , [ "/ leet / code" ], [ "/ c / d", 1], [ "/ c"]] 
output: 
[null, to true, to true, 2, to false, -1] 
explained: 
the FileSystem FileSystem = new new the FileSystem (); 

fileSystem.create ( "/ Leet",. 1); // returns to true 
fileSystem.create ( "/ Leet / code", 2); // returns to true 
fileSystem.get ( "/ Leet / code"); // returns 2 
fileSystem.create ( "/ C / D",. 1); // returns false because the parent path "/ c" does not exist. 
fileSystem.get ( "/ c"); // return -1 as the path does not exist. 

prompt:

  • Number of calls both functions together or less 10^4
  • 2 <= path.length <= 100
  • 1 <= value <= 10^9

Guess you like

Origin www.cnblogs.com/strengthen/p/11407049.html