您的位置:首页 > 其它

LeetCode之Simplify Path

2015-07-07 17:32 405 查看
/*细节模拟题。
参考自:https://github.com/soulmachine/leetcode*/
class Solution {
public:
string simplifyPath(string path) {
string res;
vector<string> path_name;
string::iterator it = path.begin();
while(it != path.end()){
++it;
string::iterator itt = find(it, path.end(), '/');
string dir(it, itt);
if(!dir.empty() && dir != "."){//dir为有效目录
if(dir == ".."){//返回上级目录
if(!path_name.empty()) path_name.pop_back();
}
else{
path_name.push_back(dir);
}
}
it = itt;
}
if(path_name.empty()) return "/";
for(int i = 0; i < path_name.size(); ++i)
res = res +  "/" + path_name[i];
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: