力扣 71 简化路径

在这里插入图片描述
在这里插入图片描述
一开始没看懂题目,所以一些典型的测试用例靠提交来获取(不断试错)/(ㄒoㄒ)/~~在这里插入图片描述
弄清楚几种情况就行了

返回上一级 /a/。。/c /c
当前目录 /a/。/c/ /a/c
重复 /a//c/ /a/c
含点的文件名 /a/。ccc。/ /a/。ccc。
含点的文件名 /a/。.。cc。。/ /a/。。ccc。。
含点的文件名 /a/。。。/ /a/。。。
含点的文件名 /a/。.。。cc。。。/ /a/。。。ccc。。。
普通文件名 /a/c/ /a/c

代码:

class Solution {
   
public:
    string simplifyPath(string path) {
   
        string res="";
        int i=1;
        while(i<path.size()){
   
            if(path[i]=='/') i++;
            else if(path[i]=='.'){
   // .开头
                i++;   
                if(i<path.size() && path[i]=='.'){
   //  ..开头
                    i++;
                    if(i<path.size()&&path[i]!='/'){
   //... 或者..其他  格式
                        string temp="..";
                        while(i<path.size()&&path[i]!='/'){
   
                            temp+=path[i];
                            i++;
                        }
                        res=res+"/"+temp;
                    }
                    else{
   //..切换到上一级
                        int j=0;
                        for(j;j<res.size();j++)
                            if(res[res.size()-1-j]=='/') break;
                        res=res.substr(0,res.size()-j-1);
                    }
                }
                else{
   //  测试点 "/.hidden"
                    if(i<path.size() && path[i]!='.' && path[i]!='/'){
    //.其他 开头
                        string temp=".";
                        while(i<path.size()  && path[i]!='/'){
   
                            temp+=path[i];
                            i++;
                        }
                        res=res+"/"+temp;
                    }
                }
            }
            else{
   //目录  "/hello../world"
                string temp="";  
                while(i<path.size()&&path[i]!='/'){
   
                    temp+=path[i];
                    i++;
                }
                res=res+"/"+temp;
            }
        }
        if(res.size()==0) return "/";//  测试点"/../"
        return res;
    }
};

在这里插入图片描述

相关推荐

  1. -简化路径

    2024-01-25 01:18:01       35 阅读
  2. leetCode71. 简化路径

    2024-01-25 01:18:01       36 阅读
  3. leetcode71简化路径

    2024-01-25 01:18:01       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-25 01:18:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-25 01:18:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-25 01:18:01       82 阅读
  4. Python语言-面向对象

    2024-01-25 01:18:01       91 阅读

热门阅读

  1. 75.实现统一异常处理和封装统一返回结果

    2024-01-25 01:18:01       59 阅读
  2. 2024年阿里云优惠云服务器新版价格整理

    2024-01-25 01:18:01       61 阅读
  3. 【嵌入式——C++】引用

    2024-01-25 01:18:01       53 阅读
  4. 论文素材:PSO算法介绍

    2024-01-25 01:18:01       54 阅读
  5. mysql单表查询练习题

    2024-01-25 01:18:01       54 阅读
  6. 在 Spring Boot 中使用事务

    2024-01-25 01:18:01       51 阅读