-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path588.txt
More file actions
109 lines (85 loc) · 2.74 KB
/
588.txt
File metadata and controls
109 lines (85 loc) · 2.74 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class FileSystem {
Map<String,Node>tree=new HashMap<>();
public FileSystem() {
}
public List<String> ls(String path) {
if(path.equals("/")){
List<String>res=new ArrayList<>();
for(String key:tree.keySet()){
res.add(key);
}
Collections.sort(res);
return res;
}
else{
List<String>res=new ArrayList<>();
String A[]=path.substring(1).split("/");
ls(A,0,tree,res);
Collections.sort(res);
return res;
}
}
public void mkdir(String path) {
String A[]=path.substring(1).split("/");
add(A,0,tree);
}
public void addContentToFile(String filePath, String content) {
String A[]=filePath.substring(1).split("/");
addContent(A,0,tree,content);
}
public String readContentFromFile(String filePath) {
String A[]=filePath.substring(1).split("/");
return readCon(A,0,tree);
}
public void ls(String A[],int index,Map<String,Node>root,List<String>cur){
String s=A[index];
if(index==A.length-1){
Node node=root.get(s);
Map<String,Node>childs=node.childs;
for(String key:childs.keySet()){
cur.add(key);
}
if(node.s.length()!=0)cur.add(s);
return;
}
ls(A,index+1,root.get(s).childs,cur);
}
public String readCon(String A[],int index,Map<String,Node>root){
String s=A[index];
if(index==A.length-1){
return root.get(s).s;
}
return readCon(A,index+1,root.get(s).childs);
}
public void add(String A[],int index,Map<String,Node>root){
if(index>=A.length)return;
String s=A[index];
if(!root.containsKey(s)){
root.put(s,new Node());
}
add(A,index+1,root.get(A[index]).childs);
}
public void addContent(String A[],int index,Map<String,Node> root,String content){
String s=A[index];
if(!root.containsKey(s)){
root.put(s,new Node());
}
if(index==A.length-1){
root.get(s).s+=content;
return;
}
addContent(A,index+1,root.get(s).childs,content);
}
class Node{
String s="";//content
Map<String,Node>childs=new HashMap<>();
}
}
/**
* Your FileSystem object will be instantiated and called as such:
* FileSystem obj = new FileSystem();
* List<String> param_1 = obj.ls(path);
* obj.mkdir(path);
* obj.addContentToFile(filePath,content);
* String param_4 = obj.readContentFromFile(filePath);
*/