标签:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
"/../"
?"/"
.‘/‘
together, such as "/home//foo/"
."/home/foo"
.
Stack String
class Solution { public: string delRedundantSlash(string in) { if(in.size() <= 1) return in; string out; out += in[0]; for(int i = 1; i < in.size(); i++) { if(in[i] == ‘/‘ && in[i-1] == ‘/‘) continue; out += in[i]; } #if 0 // delete the last ‘/‘ if ‘/‘ exist at the end if( out.size() >= 2 && out[out.size() - 1] == ‘/‘ && out[out.size() -2 != ‘/‘] ) { out.erase(out.size()-1, 1); } #endif return out; } string simplifyPath(string path) { bool isRoot = false; path = delRedundantSlash(path); if(path.size() <= 1) return path; // add the ‘/‘ at the end of path // inorder or handle this more convenient if(path[path.size()-1] != ‘/‘) path += ‘/‘; vector<string> st; string res; size_t pos = path.find(‘/‘) ; string tmpStr; while(pos != string::npos) { //cout << "path\t" << path << endl; //cout << "pos\t" << pos << endl; if(pos != 0) { tmpStr = path.substr(0, pos); if(tmpStr.size() == 1 && tmpStr == ".") ;// do nothing else if(tmpStr.size() == 2 && tmpStr == "..") { if(!st.empty()) st.pop_back(); } else st.push_back(tmpStr); } path = path.substr(pos+1); pos = path.find(‘/‘) ; } for(int i = 0; i < st.size(); i++) res += ‘/‘ + st[i]; if(res.empty()) res = "/"; return res; } };
标签:
原文地址:http://www.cnblogs.com/diegodu/p/4326326.html