- - 模拟题
class Solution {
vector<string> splite(string s){
vector<string> vs;
int last = -1;
for(int i = 0; i < s.size(); ++ i){
if(s[i] == '/'){
if(i - last > 1){
vs.push_back(s.substr(last + 1, i - last - 1));
}
last = i;
}
}
if(last != s.size() - 1)
vs.push_back(s.substr(last + 1, s.size() - last - 1));
return vs;
}
public:
string simplifyPath(string path) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> vs = splite(path);
vector<string> ans;
for(int i = 0; i < vs.size(); ++ i)
if(vs[i] == ".")
;
else if(vs[i] == ".."){
if(ans.size() > 0)
ans.pop_back();
}else
ans.push_back(vs[i]);
if(ans.size() <= 0)
return "/";
string ret = "";
for(int i = 0; i < ans.size(); ++ i)
ret += "/" + ans[i];
return ret;
}
};