标签:style blog http io ar color os sp for
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"
.#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
class Solution
{
public:
string simplifyPath(string path)
{
if(path.empty())
return "";
vector<string> ret;
string tmp;
stringstream ss(path);
while(getline(ss,tmp,‘/‘))
{
if(tmp.empty()||tmp==".")
continue;
if(tmp=="..")
{
if(!ret.empty())
ret.pop_back();
}
else
ret.push_back(tmp);
}
tmp.clear();
for(int i=0; i<(int)ret.size(); i++)
{
tmp+="/";
tmp+=ret[i];
}
if(ret.empty())
return "/";
return tmp;
}
};
int main()
{
Solution s;
string ss="";
cout<<s.simplifyPath(ss)<<endl;
}
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/wuchanming/p/4136289.html