-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign-file-system.cpp
55 lines (50 loc) · 1.44 KB
/
design-file-system.cpp
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
// ?????isfile???https://leetcode.com/submissions/detail/267061945/
class Node{
public:
unordered_map<string,Node*> um;
int content;
bool isfile=false;
};
class FileSystem {
public:
Node* root=new Node();
FileSystem() {
}
bool createPath(string path, int value) {
string parent=string(path.begin(),path.begin()+path.rfind('/'));
string filename=path.substr(path.rfind('/')+1);
// cout<<parent<<" "<<filename<<" "<<value<<endl;
istringstream ins(parent);
string t;
getline(ins,t,'/');
auto cur=root;
while(getline(ins,t,'/')){
if(cur->um.find(t)==cur->um.end()) return false;
cur=cur->um[t];
}
if(cur->um.find(filename)!=cur->um.end()) return false;
cur->um[filename]=new Node();
cur=cur->um[filename];
cur->isfile=true;
cur->content=value;
return true;
}
int get(string path) {
istringstream ins(path);
string t;
getline(ins,t,'/');
auto cur=root;
while(getline(ins,t,'/')){
if(cur->um.find(t)==cur->um.end()) return -1;
cur=cur->um[t];
}
if(!cur->isfile) return -1;
return cur->content;
}
};
/**
* Your FileSystem object will be instantiated and called as such:
* FileSystem* obj = new FileSystem();
* bool param_1 = obj->createPath(path,value);
* int param_2 = obj->get(path);
*/