给定一个文档 (Unix-style) 的完全路径,请进行路径简化。
例如,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
边界情况:
- 你是否考虑了 路径 =
"/../"
的情况?
在这种情况下,你需返回"/"
。 - 此外,路径中也可能包含多个斜杠
'/'
,如"/home//foo/"
。
在这种情况下,你可忽略多余的斜杠,返回"/home/foo"
。
#include "stdafx.h"
#include<string>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
string simplifyPath(string path) {
vector<string> s;
int j = 0;
int len = path.size();
if (len == 0) return path;
for (int i = 0; i < len; i++)
{
vector<char> temp;
while(i < len && path[i] != '/')
{
temp.push_back(path[i++]);
}
if (temp.size() > 0)
{
string stemp;
for (int k = 0; k < temp.size(); k++)
{
stemp += temp[k];
}
if (stemp.compare("..") == 0 && s.size() > 0)//如果为..则删除上层目录
{
s.erase(s.end()-1);
}
if (stemp.compare(".") != 0 && stemp.compare("..") != 0)//compare相同返回0,既不为.也不为..
{
s.push_back(stemp);
}
}
}
if (s.size() == 0) return "/";
string res;
vector<int> arr(s.size());
for (int i = 0; i < s.size(); i++)
{
res += "/";
res += s[i];
}
return res;
}
//int main()
//{
//
// string path1 = "/home/";
// string path2 = "/a/./b/../../c/";
// string path3 = "/a/../../b/../c//.//";
// string path4 = "/a//bc/d//././/..";
// string path5 = "";
// string path = simplifyPath(path5);
// getchar();
// return 0;
//
//}