-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDesign_File_System.cpp
More file actions
85 lines (76 loc) · 2.33 KB
/
Design_File_System.cpp
File metadata and controls
85 lines (76 loc) · 2.33 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
// LeetCode Biweekly Contest-7
/*
You are asked to design a file system which provides two functions:
create(path, value): Creates a new path and associates a value to it if possible and returns True. Returns False if the path
already exists or its parent path doesn't exist.
get(path): Returns the value associated with a path or returns -1 if the path doesn't exist.
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 two functions.
*/
class FileSystem {
public:
string directoryName;
vector <FileSystem*> subDirectories;
int value;
FileSystem()
{
directoryName = "";
subDirectories.clear();
value = -1;
}
bool create(string path, int value)
{
path = path.substr(1);
size_t index = path.find("/");
if(index == string::npos)
{
FileSystem *file = new FileSystem();
file->directoryName = path;
file->value = value;
subDirectories.push_back(file);
return true;
}
else
{
for(FileSystem *file : subDirectories)
{
if(file->directoryName == path.substr(0, index))
{
return file->create(path.substr(index), value);
}
}
}
return false;
}
int get(string path)
{
if(path == "")
return -1;
path = path.substr(1);
size_t index = path.find("/");
if(index == string::npos)
{
for(FileSystem *file : subDirectories)
{
if(file->directoryName == path)
return file->value;
}
}
else
{
for(FileSystem *file : subDirectories)
{
if(file->directoryName == path.substr(0, index))
return file->get(path.substr(index));
}
}
return -1;
}
};
/**
* Your FileSystem object will be instantiated and called as such:
* FileSystem* obj = new FileSystem();
* bool param_1 = obj->create(path,value);
* int param_2 = obj->get(path);
*/