标签:style blog http color get strong
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"
.class Solution { public: string simplifyPath(string path) { stack<string> s; string str; int n=path.size(); for(int i=0;i<n;i++) { if(path[i]==‘/‘) { if(str=="..") { if(!s.empty()) s.pop(); } else if(str!="."&&str!="") s.push(str); str=""; } else { str+=path[i]; } } /*处理最后字符不是以"/"结束,但是str保存了一部分字符串*/ if(str=="..") { if(!s.empty()) s.pop(); } else if(str!="."&&str!="") s.push(str); if(s.empty()) return "/"; string result; while(!s.empty()) { result="/"+s.top()+result; s.pop(); } return result; } };
标签:style blog http color get strong
原文地址:http://www.cnblogs.com/awy-blog/p/3813930.html