以 Unix 风格给出一个文件的绝对路径,你需要简化它。或者换句话说,将其转换为规范路径。
    在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。更多信息请参阅:Linux / Unix中的绝对路径 vs 相对路径
    请注意,返回的规范路径必须始终以斜杠 / 开头,并且两个目录名之间必须只有一个斜杠 /。最后一个目录名(如果存在)不能/ 结尾。此外,规范路径必须是表示绝对路径的最短字符串。

    示例 1:

    1. 输入:"/home/"
    2. 输出:"/home"
    3. 解释:注意,最后一个目录名后面没有斜杠。

    示例 2:

    1. 输入:"/../"
    2. 输出:"/"
    3. 解释:从根目录向上一级是不可行的,因为根是你可以到达的最高级。

    示例 3:

    1. 输入:"/home//foo/"
    2. 输出:"/home/foo"
    3. 解释:在规范路径中,多个连续斜杠需要用一个斜杠替换。

    示例 4:

    1. 输入:"/a/./b/../../c/"
    2. 输出:"/c"

    示例 5:

    1. 输入:"/a/../../b/../c//.//"
    2. 输出:"/c"

    示例 6:

    1. 输入:"/a//b////c/d//././/.."
    2. 输出:"/a/b/c"
    1. class Solution {
    2. public:
    3. string simplifyPath(string path) {
    4. if(path.size() == 0){
    5. return path;
    6. }
    7. stack<string> r_path;
    8. int left = 0;
    9. r_path.push("/");
    10. for(int i=0;i<path.size();i++){
    11. cout<<"i: "<<i<<endl;
    12. if(path[i] == '/'){
    13. cout<<left<<" "<<i<<endl;
    14. if(i - left - 1> 0){
    15. string temp = path.substr(left+1, i - left-1);
    16. cout<<temp<<endl;
    17. if(temp == "."){
    18. }else if(temp == ".."){
    19. r_path.pop();
    20. if(!r_path.empty())
    21. r_path.pop();
    22. if(r_path.empty()){
    23. r_path.push("/");
    24. }
    25. }else{
    26. r_path.push(temp);
    27. r_path.push("/");
    28. }
    29. }
    30. left = i;
    31. }
    32. }
    33. if(path.size() - left - 1> 0){
    34. string temp = path.substr(left+1, path.size() - left-1);
    35. cout<<temp<<endl;
    36. if(temp == "."){
    37. }else if(temp == ".."){
    38. r_path.pop();
    39. if(!r_path.empty())
    40. r_path.pop();
    41. if(r_path.empty()){
    42. r_path.push("/");
    43. }
    44. }else{
    45. r_path.push(temp);
    46. r_path.push("/");
    47. }
    48. }
    49. if(r_path.top() == "/"){
    50. r_path.pop();
    51. if(r_path.empty()){
    52. r_path.push("/");
    53. }
    54. }
    55. string res;
    56. while(!r_path.empty()){
    57. res.insert(0, r_path.top());
    58. r_path.pop();
    59. }
    60. return res;
    61. }
    62. };