标签:
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) { int N=path.size(); stack<string> s; for(int i=0;i<N;) { while(i<N&&path[i]=='/') //遇到/跳过 i++; string temp=""; //记录字符串 while(i<N&&path[i]!='/') { temp.push_back(path[i]); i++; } if(temp==".."&&s.empty()==0) s.pop(); else if(temp!=""&&temp!=".."&&temp!=".") s.push(temp); } if(s.empty()) return "/"; string res=""; while(!s.empty()) { res="/"+s.top()+res; s.pop(); } return res; } };
标签:
原文地址:http://blog.csdn.net/u011391629/article/details/52105367