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
思路:如果只是上面的要求,那么hashmap就可以解决;
class FileSystem {
private HashMap<String, Integer> hashmap;
public FileSystem() {
this.hashmap = new HashMap<>();
hashmap.put("", -1);
}
public boolean createPath(String path, int value) {
int index = path.lastIndexOf("/");
String parent = path.substring(0, index);
if(!hashmap.containsKey(parent)) {
return false;
}
return hashmap.putIfAbsent(path, value) == null;
}
public int get(String path) {
return hashmap.getOrDefault(path, -1);
}
}
/**
* Your FileSystem object will be instantiated and called as such:
* FileSystem obj = new FileSystem();
* boolean param_1 = obj.createPath(path,value);
* int param_2 = obj.get(path);
*/
但是,Doordash的onsite有delete的要求,如果path有children就不能delete;
- delete(path) -> deletes a node, but ONLY if it has no children
如果用hashmap,那么delete就要loop所有的key,不是很efficient;那么如果用trie,只用search这个node以下的部分就可以了;所以这个题就是个变种的trie; 弄清楚了,其实也还好写;
class FileSystem {
class TrieNode {
public int value;
public HashMap<String, TrieNode> children;
public TrieNode() {
this.value = -1;
this.children = new HashMap<>();
}
}
private TrieNode root;
public FileSystem() {
this.root = new TrieNode();
}
public boolean createPath(String path, int value) {
TrieNode cur = root;
String[] splits = path.split("/");
int n = splits.length;
// parent path; len - 1;
// 注意是从1开始的,因为前面有个空格,不需要;
for(int i = 1; i < n - 1; i++) {
String word = splits[i];
if(!cur.children.containsKey(word)) {
return false;
}
cur = cur.children.get(word);
}
// last word;
String lastword = splits[n - 1];
if(cur.children.containsKey(lastword)) {
return false;
}
cur.children.put(lastword, new TrieNode());
TrieNode node = cur.children.get(lastword);
node.value = value;
return true;
}
public int get(String path) {
TrieNode cur = root;
String[] splits = path.split("/");
// 注意是从1开始的,因为前面有个空格,不需要;
for(int i = 1; i < splits.length; i++) {
String word = splits[i];
if(!cur.children.containsKey(word)) {
return -1;
}
cur = cur.children.get(word);
}
return cur.value;
}
public boolean deletePath(String path) {
TrieNode cur = root;
String[] splits = path.split("/");
// 注意是从1开始的,因为前面有个空格,不需要;
for(int i = 1; i < splits.length - 1; i++) {
String word = splits[i];
if(!cur.children.containsKey(word)) {
return false;
}
cur = cur.children.get(word);
}
//last word;
String lastword = splits[splits.length - 1];
TrieNode lastnode = cur.children.get(lastword);
if(lastnode.children.size() == 0) {
cur.children.remove(lastword);
return true;
} else {
return false;
}
}
}
/**
* Your FileSystem object will be instantiated and called as such:
* FileSystem obj = new FileSystem();
* boolean param_1 = obj.createPath(path,value);
* int param_2 = obj.get(path);
*/