前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 71. Simplify Path

LeetCode 71. Simplify Path

作者头像
ShenduCC
发布2019-09-26 10:43:16
2960
发布2019-09-26 10:43:16
举报
文章被收录于专栏:算法修养算法修养算法修养

题目

字符串问题

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;
        
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档