71. Simplify Path

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".

class Solution {
public:
string simplifyPath(string path) {
stack<string> mystack;

int p = 0;
while (path[p] == '/'){
p++;
}
if (p == path.size())
return "/";
path = path.substr(p);

string s = spilt(path, '/');
while (!s.empty()){
if (s.compare(".") == 0){
//do nothing

}
else if (s.compare("..") == 0){
if (!mystack.empty())
mystack.pop();
}
else
mystack.push(s);

int p = 0;
while (path[p] == '/'){
p++;
}
path = path.substr(p);
s = spilt(path, '/');
}
if (mystack.empty()){
return "/";
}
vector<string> simp(mystack.size());
for (int i = 0; i < simp.size(); i++){
simp[i] = mystack.top();
mystack.pop();
}
string res;
for (int i = simp.size()-1; i >= 0; i--){
res += "/" + simp[i];
}
return res;
}
string spilt(string& path, char ch){
string s;
int i;
int size = path.size();
for (i = 0; i < path.size(); i++){
char c = path[i];
if (c == ch){
s = path.substr(0, i);
path = path.substr(i + 1);
break;
}
}
if (i == size){
s = path;
path.clear();
}
return s;
}
};


LeetCode --- 71. Simplify Path

2015-03-20 21:46:14

Leetcode 71. Simplify Path 简化路径 解题报告

2016-05-13 23:52:55

71. Simplify Path | Java最短代码实现

2016-03-19 23:42:02

leetcode_c++：栈：Simplify Path（071）

2016-07-20 16:11:35

LeetCode71 Simplify Path java题解

2015-07-01 08:11:03

【leetcode】71. Simplify Path【java】

2016-12-19 11:20:26

71. Simplify Path(Python3)

2018-01-28 09:41:38

leetcode No71. Simplify Path

2016-08-03 17:14:08

71. Simplify Path(java)

2018-02-07 08:56:44

LeetCode71——Simplify Path

2015-11-03 09:24:55

不良信息举报

71. Simplify Path