将输入路径简化了unix风格的路径。栈即可解决。
class Solution { public: string simplifyPath(string path) { vector<char <em>>paths; paths.clear(); char * split = strtok(const_cast<char</em>>(path.c_str()),"/"); while(split != NULL) { paths.push_back(split); split = strtok(NULL,"/"); } stack<char *>st; for(int i = 0;i < paths.size();i++) { if(strcmp(paths[i],".") == 0)continue; if(strcmp(paths[i],"..") == 0) { if(!st.empty())st.pop(); } else { st.push(paths[i]); } } paths.clear(); while(!st.empty()) { paths.push_back(st.top()); st.pop(); } string spath = ""; for(int i = paths.size()-1;i >= 0;i--) { spath = spath + string("/"); spath = spath + string(paths[i]); } if(spath == "")<br /> spath = spath + string("/"); return spath; } };