LC: 1166. Design File System
https://leetcode.com/problems/design-file-system/
1166. Design File System
You are asked to design a file system that allows you to create new paths and associate them with different values.
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 FileSystem class:
bool createPath(string path, int value)Creates a newpathand associates avalueto it if possible and returnstrue. Returnsfalseif the path already exists or its parent path doesn't exist.int get(string path)Returns the value associated withpathor returns-1if the path doesn't exist.
Example 1:
Input:
["FileSystem","createPath","get"]
[[],["/a",1],["/a"]]
Output:
[null,true,1]
Explanation:
FileSystem fileSystem = new FileSystem();
fileSystem.createPath("/a", 1); // return true
fileSystem.get("/a"); // return 1Example 2:
Input:
["FileSystem","createPath","createPath","get","createPath","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.createPath("/leet", 1); // return true
fileSystem.createPath("/leet/code", 2); // return true
fileSystem.get("/leet/code"); // return 2
fileSystem.createPath("/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
104in total.2 <= path.length <= 1001 <= value <= 109
The Essence:
Um die Dateien und die Verzeichnisse genau zu verarbeiten, brauchen wir eine Datenstruktur, die dynamisch zu updaten ist. Da die Dateipfade als String auftreten, muss der Problemlöser sie parsen. Das Schrägstrich in den Dateipfaden impliziert ein Eltern-Kind-Beziehung zwischen den Verzeichnissen und Dateien, was der Problemlöser in Betracht ziehen soll.
Details:
Hashtabellen sind das primäre Hilfsmittel für die Lösung, das sie Strings als Schlüssel benutzen. Die String-Funktionen der Programmiersprachen sind bei dem Parsen ebenfalls hilfreich.
Solution(s):
Default Code:
Last updated