diff --git a/solution/0000-0099/0071.Simplify Path/README.md b/solution/0000-0099/0071.Simplify Path/README.md index 2c56c20181294..a9d560225da1f 100644 --- a/solution/0000-0099/0071.Simplify Path/README.md +++ b/solution/0000-0099/0071.Simplify Path/README.md @@ -226,10 +226,27 @@ function simplifyPath(path: string): string { } ``` -### **...** - -``` +### **C++** +```cpp +class Solution { +public: + string simplifyPath(string path) { + deque stk; + string res, tmp; + stringstream ss(path); + while (getline(ss, tmp, '/')) { + if (tmp == "" || tmp == ".") continue; + if (tmp == "..") { + if (!stk.empty()) + stk.pop_back(); + } else stk.push_back(tmp); + } + for (auto str: stk) + res += "/" + str; + return res.empty() ? "/" : res; + } +}; ``` diff --git a/solution/0000-0099/0071.Simplify Path/README_EN.md b/solution/0000-0099/0071.Simplify Path/README_EN.md index 50e436b6463e4..a1e2a9050fd09 100644 --- a/solution/0000-0099/0071.Simplify Path/README_EN.md +++ b/solution/0000-0099/0071.Simplify Path/README_EN.md @@ -210,10 +210,27 @@ function simplifyPath(path: string): string { } ``` -### **...** - -``` +### **C++** +```cpp +class Solution { +public: + string simplifyPath(string path) { + deque stk; + string res, tmp; + stringstream ss(path); + while (getline(ss, tmp, '/')) { + if (tmp == "" || tmp == ".") continue; + if (tmp == "..") { + if (!stk.empty()) + stk.pop_back(); + } else stk.push_back(tmp); + } + for (auto str: stk) + res += "/" + str; + return res.empty() ? "/" : res; + } +}; ``` diff --git a/solution/0000-0099/0071.Simplify Path/Solution.cpp b/solution/0000-0099/0071.Simplify Path/Solution.cpp new file mode 100644 index 0000000000000..ff588d90596f7 --- /dev/null +++ b/solution/0000-0099/0071.Simplify Path/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + string simplifyPath(string path) { + deque stk; + string res, tmp; + stringstream ss(path); + while (getline(ss, tmp, '/')) { + if (tmp == "" || tmp == ".") continue; + if (tmp == "..") { + if (!stk.empty()) + stk.pop_back(); + } else stk.push_back(tmp); + } + for (auto str: stk) + res += "/" + str; + return res.empty() ? "/" : res; + } +};