-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplify Path.cpp
More file actions
71 lines (58 loc) · 1.05 KB
/
Simplify Path.cpp
File metadata and controls
71 lines (58 loc) · 1.05 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
class Solution {
public:
string parseWord(string path, int &index, int length)
{
bool findWords = false;
string word;
index++;
while(index < length)
{
if(path.at(index) == '/')
break;
else
word.push_back(path.at(index++));
}
return word;
}
string simplifyPath(string path) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
stack<string> stackStr;
int length = path.size();
int i = 0;
if(length <= 0)
return "";
do{
string word = parseWord(path, i, length);
if(word == "..")
{
if(!stackStr.empty())
stackStr.pop();
}else if(word == ".")
continue;
else if(word.size() > 0)
{
stackStr.push(word);
}
}while(i<length);
if(stackStr.empty())
return "/";
string result;
while(!stackStr.empty())
{
result.insert(0, "/"+stackStr.top());
stackStr.pop();
}
return result;
}
};
//void main()
//{
// Solution s;
// string str;
// while(cin>>str)
// {
// cout<<s.simplifyPath(str)<<endl;
// }
// getchar();
//}