2022-3-19 Leetcode 71. 简化路径

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
第一版

class Solution {
public:
    string simplifyPath(string path) {
        vector<string> mysk;
        string ret;
        int i = 0;
        int len = path.size();
        while(i < len){
            int count = 0;
            int alpha = 0;
            string word;
            while(i < len && path[i] == '.'){
                count++;
                i++;
            }
            if(count == 2){
                if(!mysk.empty())
                mysk.pop_back();
            }else if(count > 2){
                mysk.push_back(string(count,'.'));
            }
            while(isalpha(path[i]) || path[i] == '_'){
                alpha++;
                i++;
            }
            word = path.substr(i-alpha,alpha);
            if(!word.empty())
            mysk.push_back(word);
            i++;
        }
        if(mysk.empty()){
            return "/";
        }
        for(i = 0;i < mysk.size();i++){
            ret += ("/" +mysk[i]);
        }
        return ret;
    }
};

在这里插入图片描述
第二版
在这里插入图片描述

class Solution {
public:
    string simplifyPath(string path) {
        vector<string> mysk;
        string ret;
        int i = 0;
        int len = path.size();
        while(i < len){
            int count = 0;
            int alpha = 0;
            string word;
            while(i < len && path[i] == '.'){
                count++;
                i++;
            }
            if(count == 2 && (i == len || path[i] == '/')&& i > 2 && path[i-3] == '/'){
                if(!mysk.empty())
                mysk.pop_back();
            }else if(count > 2 && path[i] == '/'){
                mysk.push_back(string(count,'.'));
            }else if(count == 1 && (i == len || path[i] == '/')){

            }else {
                word += string(count,'.');
            }
            
            while(isalpha(path[i]) || path[i] == '_'){
                alpha++;
                i++;
            }
            word += path.substr(i-alpha,alpha);
            if(!word.empty())
            mysk.push_back(word);
            i++;
        }
        if(mysk.empty()){
            return "/";
        }
        for(i = 0;i < mysk.size();i++){
            ret += ("/" +mysk[i]);
        }
        return ret;
    }
};

第三版

class Solution {
public:
    string simplifyPath(string path) {
        vector<string> mysk;
        string ret;
        while(!path.empty()){
            string word;
            int cur = path.find('/');
            if(cur == string::npos){
                word = path;
            }
            word = path.substr(0,cur);
            if(word == "."){
                word = "";
          //  }else if(word == ".." && !mysk.empty()){
            }else if(word == ".."){
                if(!mysk.empty())
                mysk.pop_back();
                word = "";
            }else if(!word.empty()){
                mysk.push_back(word);
            }
            path.erase(0,cur+1);
            if(cur == string::npos)
            break;
        }
        if(mysk.empty()){
            return "/";
        }
        for(int i = 0;i < mysk.size();i++){
            ret += ('/' + mysk[i]);
        }
        return ret;
    }
};

2022-7-19的一版

class Solution {
public:
    string simplifyPath(string path) {
        deque<string> m_stack;
        int i = 0; 
        int len = path.size();
        for (; i < len; i++){
            while (i < len && path[i] == '/') i++;
            int j = i;
            while (j < len && path[j] != '/') j++;
            string str = path.substr(i, j - i);
            if (str == ".."){
                if (!m_stack.empty())
                m_stack.pop_back();
            } else if (str != "" && str != "."){
                m_stack.push_back(str);
            }
            i = j;
        }
        string ans;
        while (!m_stack.empty()){
            ans += '/';
            string str = m_stack.front();
            m_stack.pop_front();
            ans += str;
        }
        return ans == "" ? "/" : ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值