Skip to content

Commit 86bba25

Browse files
authored
feat: add cpp solution to lc problem: No.0071 (doocs#744)
1 parent bb0514b commit 86bba25

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

solution/0000-0099/0071.Simplify Path/README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,27 @@ function simplifyPath(path: string): string {
226226
}
227227
```
228228

229-
### **...**
230-
231-
```
229+
### **C++**
232230

231+
```cpp
232+
class Solution {
233+
public:
234+
string simplifyPath(string path) {
235+
deque<string> stk;
236+
string res, tmp;
237+
stringstream ss(path);
238+
while (getline(ss, tmp, '/')) {
239+
if (tmp == "" || tmp == ".") continue;
240+
if (tmp == "..") {
241+
if (!stk.empty())
242+
stk.pop_back();
243+
} else stk.push_back(tmp);
244+
}
245+
for (auto str: stk)
246+
res += "/" + str;
247+
return res.empty() ? "/" : res;
248+
}
249+
};
233250
```
234251
235252
<!-- tabs:end -->

solution/0000-0099/0071.Simplify Path/README_EN.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,27 @@ function simplifyPath(path: string): string {
210210
}
211211
```
212212

213-
### **...**
214-
215-
```
213+
### **C++**
216214

215+
```cpp
216+
class Solution {
217+
public:
218+
string simplifyPath(string path) {
219+
deque<string> stk;
220+
string res, tmp;
221+
stringstream ss(path);
222+
while (getline(ss, tmp, '/')) {
223+
if (tmp == "" || tmp == ".") continue;
224+
if (tmp == "..") {
225+
if (!stk.empty())
226+
stk.pop_back();
227+
} else stk.push_back(tmp);
228+
}
229+
for (auto str: stk)
230+
res += "/" + str;
231+
return res.empty() ? "/" : res;
232+
}
233+
};
217234
```
218235
219236
<!-- tabs:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
string simplifyPath(string path) {
4+
deque<string> stk;
5+
string res, tmp;
6+
stringstream ss(path);
7+
while (getline(ss, tmp, '/')) {
8+
if (tmp == "" || tmp == ".") continue;
9+
if (tmp == "..") {
10+
if (!stk.empty())
11+
stk.pop_back();
12+
} else stk.push_back(tmp);
13+
}
14+
for (auto str: stk)
15+
res += "/" + str;
16+
return res.empty() ? "/" : res;
17+
}
18+
};

0 commit comments

Comments
 (0)