LeetCode 71. Simplify Path

题目

字符串问题

class Solution {
public:
    string simplifyPath(string path) {
        
        string paths[10005];
        int pos=0;
        paths[0]="/";
        string str="";
        for(int i=0;i<path.length();i++)
        {
            if(i==0&&path[i]=='/')
                continue;
            
            if(path[i]=='/')
            {
                if(str=="")
                    continue;
                if(str=="..")
                {
                    if(pos>0){
                        pos--;
                    }
                    str="";
                    continue;
                }
                else if(str==".")
                {
                    str="";
                    continue;
                }
                else
                {
                    str+='/';
                    paths[++pos]=str;
                    str="";
                }
              
            }
            else {
                
                str+=path[i];
            }
        }
        if(str=="..")
        {
            if(pos>0){
            pos--;
            }
        }
        else if(str!=""&&str!=".")
        {
            str+='/';
            paths[++pos]=str;
        }
        
        if(pos!=0){
            
            paths[pos]=paths[pos].substr(0,paths[pos].length()-1);
        }
        
        string ans="";
        for(int i=0;i<=pos;i++)
        {
            ans+=paths[i];
        }
        return ans;
        
    }
};
posted @   Shendu.CC  阅读(113)  评论(0编辑  收藏  举报
编辑推荐:
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
阅读排行:
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
点击右上角即可分享
微信分享提示