File tree Expand file tree Collapse file tree 3 files changed +58
-6
lines changed
solution/0000-0099/0071.Simplify Path Expand file tree Collapse file tree 3 files changed +58
-6
lines changed Original file line number Diff line number Diff line change @@ -226,10 +226,27 @@ function simplifyPath(path: string): string {
226
226
}
227
227
```
228
228
229
- ### ** ...**
230
-
231
- ```
229
+ ### ** C++**
232
230
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
+ };
233
250
```
234
251
235
252
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -210,10 +210,27 @@ function simplifyPath(path: string): string {
210
210
}
211
211
```
212
212
213
- ### ** ...**
214
-
215
- ```
213
+ ### ** C++**
216
214
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
+ };
217
234
```
218
235
219
236
<!-- tabs:end -->
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments