-
Notifications
You must be signed in to change notification settings - Fork 384
Expand file tree
/
Copy pathDesign File System.java
More file actions
57 lines (49 loc) · 1.36 KB
/
Copy pathDesign File System.java
File metadata and controls
57 lines (49 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class FileSystem {
private FileNode root;
public FileSystem() {
this.root = new FileNode();
}
public boolean createPath(String path, int value) {
String[] split = path.split("/");
FileNode node = root;
for (int i = 1; i < split.length - 1; i++) {
if (!node.children.containsKey(split[i])) {
return false;
}
node = node.children.get(split[i]);
}
if (node.children.containsKey(split[split.length - 1])) {
return false;
}
node.children.put(split[split.length - 1], new FileNode(value));
return true;
}
public int get(String path) {
String[] split = path.split("/");
FileNode node = root;
for (int i = 1; i < split.length; i++) {
if (!node.children.containsKey(split[i])) {
return -1;
}
node = node.children.get(split[i]);
}
return node.value == null ? -1 : node.value;
}
private static class FileNode {
private final Map<String, FileNode> children;
private Integer value;
public FileNode() {
this.children = new HashMap<>();
}
public FileNode(Integer value) {
this.children = new HashMap<>();
this.value = value;
}
}
}
/**
* 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);
*/