-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1166. Design File System.cpp
More file actions
163 lines (123 loc) · 4.25 KB
/
1166. Design File System.cpp
File metadata and controls
163 lines (123 loc) · 4.25 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// class FileSystem {
// // The TrieNode data structure.
// class TrieNode {
// String name;
// int val = -1;
// Map<String, TrieNode> map = new HashMap<>();
// TrieNode (String name) {
// this.name = name;
// }
// }
// TrieNode root;
// // Root node contains the empty string.
// public FileSystem() {
// this.root = new TrieNode("");
// }
// public boolean createPath(String path, int value) {
// // Obtain all the components
// String[] components = path.split("/");
// // Start "curr" from the root node.
// TrieNode cur = root;
// // Iterate over all the components.
// for (int i = 1; i < components.length; i++) {
// String currentComponent = components[i];
// // For each component, we check if it exists in the current node's dictionary.
// if (cur.map.containsKey(currentComponent) == false) {
// // If it doesn't and it is the last node, add it to the Trie.
// if (i == components.length - 1) {
// cur.map.put(currentComponent, new TrieNode(currentComponent));
// } else {
// return false;
// }
// }
// cur = cur.map.get(currentComponent);
// }
// // Value not equal to -1 means the path already exists in the trie.
// if (cur.val != -1) {
// return false;
// }
// cur.val = value;
// return true;
// }
// public int get(String path) {
// // Obtain all the components
// String[] components = path.split("/");
// // Start "curr" from the root node.
// TrieNode cur = root;
// // Iterate over all the components.
// for (int i = 1; i < components.length; i++) {
// String currentComponent = components[i];
// // For each component, we check if it exists in the current node's dictionary.
// if (cur.map.containsKey(currentComponent) == false) {
// return -1;
// }
// cur = cur.map.get(currentComponent);
// }
// return cur.val;
// }
// }
class FileSystem {
class TrieNode {
public:
string name;
int val = -1;
unordered_map<string, TrieNode*> mp;
TrieNode(string name, int val) {
this->name = name;
this->val = val;
}
};
vector<string> split(string const& input, char delimiter) {
vector<string> tokens;
istringstream stream(input);
string token;
while (getline(stream, token, delimiter)) {
if(token.empty()) continue;
tokens.push_back(token);
}
return tokens;
}
TrieNode* root;
public:
FileSystem() {
this->root = new TrieNode("", -1);
}
bool createPath(string path, int value) {
vector<string> dirs = split(path, '/');
TrieNode* cur = root;
for(int i = 0; i < dirs.size(); i++) {
string dir = dirs[i];
if(cur->mp.count(dir)) {
cur = cur->mp[dir];
}
else {
if(i == dirs.size() - 1) {
cur->mp[dir] = new TrieNode(dir, value);
return true;
}
return false;
}
}
if(cur->val != -1) return false;
cur->val = value;
return true;
}
int get(string path) {
vector<string> dirs = split(path, '/');
TrieNode* cur = root;
for(int i = 0; i < dirs.size(); i++) {
string dir = dirs[i];
if(cur->mp.count(dir)) {
cur = cur->mp[dir];
}
else return -1;
}
return cur->val;
}
};
/**
* 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);
*/