Skip to content

Commit 1d38742

Browse files
authored
Create design-file-system.cpp
1 parent 8314568 commit 1d38742

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

C++/design-file-system.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time: create: O(n)
2+
// get: O(n)
3+
// Space: O(n)
4+
5+
class FileSystem {
6+
public:
7+
FileSystem() : lookup_({{"", -1}}) {
8+
9+
}
10+
11+
bool create(string path, int value) {
12+
if (!lookup_.count(path.substr(0, path.find_last_of('/')))) {
13+
return false;
14+
}
15+
lookup_[path] = value;
16+
return true;
17+
}
18+
19+
int get(string path) {
20+
if (!lookup_.count(path)) {
21+
return -1;
22+
}
23+
return lookup_[path];
24+
}
25+
26+
private:
27+
unordered_map<string, int> lookup_;
28+
};

0 commit comments

Comments
 (0)