classFileSystem { structNode { unordered_map<string, shared_ptr<Node>> children; int value; Node(int v) : value(v) {} }; vector<string> split(const string& path){ string tmp; vector<string> stk; stringstream ss(path); while(getline(ss,tmp,'/')) { stk.push_back(tmp); } return stk; } shared_ptr<Node> m; public: FileSystem() { m = make_shared<Node>(0); } boolcreate(string path, int value){ auto words = split(path); auto current = m; for (int i = 1; i < words.size() - 1; ++i) { constauto& word = words[i]; if (current->children.find(word) == current->children.end()) { returnfalse; } else { current = current->children[word]; } } if (current->children.find(words.back()) != current->children.end()) { returnfalse; } current->children[words.back()] = make_shared<Node>(value); returntrue; } intget(string path){ auto words = split(path); auto current = m; for (int i = 1; i < words.size(); ++i) { constauto& word = words[i]; if (current->children.find(word) == current->children.end()) { return-1; } else { current = current->children[word]; } } return current->value; } };
/** * 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); */