LeetCode 1166. Design File System
原题链接在这里:https://leetcode.com/problems/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 newpath
and associates avalue
to it if possible and returnstrue
. Returnsfalse
if the path already exists or its parent path doesn't exist.int get(string path)
Returns the value associated withpath
or returns-1
if 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 1
Example 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
104
in total. 2 <= path.length <= 100
1 <= value <= 109
题解:
Main a map between path and value.
When createPath, get parent of path, if it doen't exist in the map, return false.
Otherwise, map.putIfAbsent(path, value). if putIfAbsent doesn't return null, it means path already exists in the map, return false.
Get is to simply return the value from the map.
Time Complexity: craetePath, O(n). n = path.length. get, O(1).
Space: O(m*n). m = map.size(). n = path length.
AC Java:
1 class FileSystem { 2 Map<String, Integer> map; 3 4 public FileSystem() { 5 map = new HashMap<>(); 6 map.put("", -1); 7 } 8 9 public boolean createPath(String path, int value) { 10 int ind = path.lastIndexOf("/"); 11 if(ind == -1){ 12 return false; 13 } 14 15 String parent = path.substring(0, ind); 16 if(!map.containsKey(parent)){ 17 return false; 18 } 19 20 return map.putIfAbsent(path, value) == null; 21 } 22 23 public int get(String path) { 24 return map.getOrDefault(path, -1); 25 } 26 } 27 28 /** 29 * Your FileSystem object will be instantiated and called as such: 30 * FileSystem obj = new FileSystem(); 31 * boolean param_1 = obj.createPath(path,value); 32 * int param_2 = obj.get(path); 33 */
Could solve it use Trie as well.
Time Complexity: craetePath, O(n). get, O(n). n = path.length.
Space: O(m). m is trie tree size.
AC Java:
1 class FileSystem { 2 TrieNode root; 3 4 public FileSystem() { 5 root = new TrieNode(); 6 } 7 8 public boolean createPath(String path, int value) { 9 TrieNode cur = root; 10 String [] arr = path.split("/"); 11 for(int i = 1; i < arr.length; i++){ 12 if(!cur.nexts.containsKey(arr[i])){ 13 if(i == arr.length - 1){ 14 cur.nexts.put(arr[i], new TrieNode()); 15 }else{ 16 return false; 17 } 18 } 19 20 cur = cur.nexts.get(arr[i]); 21 } 22 23 if(cur.val != null){ 24 return false; 25 } 26 27 cur.val = value; 28 return true; 29 } 30 31 public int get(String path) { 32 TrieNode cur = root; 33 String [] arr = path.split("/"); 34 for(int i = 1; i < arr.length; i++){ 35 if(!cur.nexts.containsKey(arr[i])){ 36 return -1; 37 } 38 39 cur = cur.nexts.get(arr[i]); 40 } 41 42 return cur.val == null ? -1 : cur.val; 43 } 44 } 45 46 class TrieNode{ 47 Map<String, TrieNode> nexts; 48 Integer val; 49 50 public TrieNode(){ 51 nexts = new HashMap<>(); 52 } 53 } 54 55 /** 56 * Your FileSystem object will be instantiated and called as such: 57 * FileSystem obj = new FileSystem(); 58 * boolean param_1 = obj.createPath(path,value); 59 * int param_2 = obj.get(path); 60 */