-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#123.cc
More file actions
36 lines (33 loc) · 891 Bytes
/
Copy pathLeetCode#123.cc
File metadata and controls
36 lines (33 loc) · 891 Bytes
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
class Solution {
public:
string simplifyPath(string path) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> vec;
string str = "";
for(int i=0;i<path.length();i++){
if(path[i]=='/'){
if(str!="") vec.push_back(str);
str="";
}
else str+=path[i];
}
if(str!="") vec.push_back(str);
int ind = 0;
for(int i=0;i<vec.size();i++){
if(vec[i]==".."){
if(ind>0) --ind;
}
else if(vec[i]!="."){
vec[ind++]=vec[i];
}
}
if(ind==0) return "/";
else{
string ret="";
for(int i=0;i<ind;i++)
ret += "/"+vec[i];
return ret;
}
}
};