题目:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
分析:
代码:
class Solution {
public:
string simplifyPath(string path) {
string res="", tmp;
vector<string> stk;
stringstream sstm(path);
while(getline(sstm,tmp,'/')){
if(tmp==""||tmp==".")continue;
if(tmp==".."&&!stk.empty())stk.pop_back();
else if(tmp!="..")stk.push_back(tmp);
}
for(auto st:stk)res+="/"+st;
return res.empty()?"/":res;
}
};