forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplify_Path.cpp
More file actions
75 lines (68 loc) · 1.77 KB
/
Simplify_Path.cpp
File metadata and controls
75 lines (68 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 18, 2012
Problem: Simplify Path
Difficulty: medium
Source: http://www.leetcode.com/onlinejudge
Notes:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
Corner Cases:
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together,
such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".
Solution:
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <stack>
using namespace std;
class Solution {
public:
string simplifyPath(string path) {
vector<string> simple;
int start = 1;
simple.push_back("/");
for (unsigned i = 1; i < path.length(); i++) {
if (path[i] == '/') {
string level = path.substr(start, i - start + 1);
if (level.compare("../") == 0) {
if (simple.size() > 1) {
simple.pop_back();
}
} else if (level.compare("./") != 0
&& level.compare("/") != 0) {
simple.push_back(level);
}
start = i + 1;
} else if (i == path.length() - 1) {
string level = path.substr(start, i - start + 1);
if (level.compare("..") == 0) {
if (simple.size() > 1) {
simple.pop_back();
}
} else if (level.compare(".") != 0) {
simple.push_back(level);
}
}
}
string result;
for (unsigned i = 0; i < simple.size(); i++) {
result += simple[i];
}
if (result.length() > 1 && result[result.length() - 1] == '/') {
result = result.substr(0, result.length() - 1);
}
return result;
}
};