aboutsummaryrefslogtreecommitdiff
path: root/other/simplify_path.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'other/simplify_path.cpp')
-rw-r--r--other/simplify_path.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/other/simplify_path.cpp b/other/simplify_path.cpp
new file mode 100644
index 0000000..09852f3
--- /dev/null
+++ b/other/simplify_path.cpp
@@ -0,0 +1,33 @@
+// 71. Simplify Path
+#include "leetcode.h"
+
+// given a string 'path', which is an absolute path (starting with a slash) to a
+// file or directory in a unix-style file system, convert it to the simplified
+// canonical path
+
+class Solution {
+public:
+ string simplifyPath(string path) {
+ vector<string> stack;
+ string ans, tmp;
+ stringstream p(path);
+ while (getline(p, tmp, '/')) {
+ if (tmp == "" || tmp == ".")
+ continue;
+ if (!stack.empty() && tmp == "..")
+ stack.pop_back();
+ else if (tmp != "..")
+ stack.push_back(tmp);
+ }
+ for (auto s : stack)
+ ans += "/" + s;
+ return stack.empty() ? "/" : ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ cout << obj.simplifyPath("/home/") << endl; // expect: /home/
+ cout << obj.simplifyPath("/../") << endl; // expect: /
+ cout << obj.simplifyPath("/home//foo/") << endl; // expect: /home/foo
+}