-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simplify Path.cpp
44 lines (42 loc) · 1.04 KB
/
Simplify Path.cpp
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
class Solution {
private:
string getNext(string p, int &pos) {
string ret = "";
while(pos<p.length() && p[pos]!='/') {
ret+=p[pos];
pos++;
}
pos++;
return ret;
}
public:
string simplifyPath(string path) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> stack;
stack.clear();
int pos = 0;
while (pos<path.length()){
string x = getNext(path, pos);
if (x=="" || x=="."){
continue;
}
if (x==".." ) {
if (stack.size()>0)
stack.pop_back();
}
else {
stack.push_back(x);
}
}
if (stack.size()==0) {
return "/";
}
string ret = "";
for (int i=0; i<stack.size(); i++) {
ret += "/";
ret += stack[i];
}
return ret;
}
};