Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
注意特例: /... 保留 /// /.../. -> /... 按照规则进行处理
- string simplifyPath(string path) {
- stringstream is(path);
- string dir;
- vector<string> dirlst;
- while(getline(is,dir,'/')){
- if(dir == "..") {
- if(dirlst.size()!=0) {
- dirlst.pop_back();
- }
- } else {
- if(dir.size()!=0 && dir!=".") {
- dirlst.push_back(dir);
- }
- }
- }
- string res = "";
- if(dirlst.size()==0) {
- res += "/";
- } else {
- for(int i=0;i<dirlst.size();i++) {
- res += '/';
- res += dirlst[i];
- }
- }
- return res;
- }