71-简化路径

#include <string>
#include <vector>
#include <iostream>

using namespace std;

class Solution {
public:

/*
从string的开头处开始访问
    如果碰到斜杠
        继续
    如果是char
        循环直到碰到斜杠
            记录进入temp
        如果temp=="."
            继续
        如果temp == ".."
            如果final_result不为空
                final_result pop最后两个值
            继续
        把temp push进去
如果final_result 为空
    返回"/"
如果final_result不为空
    循环加入 "/" 和 字符串
*/

};

string simplifyPath(string path) {
    vector<string> final_result;
    for(int i = 0; i < path.size(); i++){
        if(path[i] == '/'){
            continue;
        }else{
            string temp;
            temp += path[i];
            while (++i < path.size() && path[i] != '/')
                temp += path[i];
            i--;
            if(temp == ".")
                continue;
            if(temp == ".."){
                if(!final_result.empty()){
                    final_result.pop_back();
                }
                continue;
            }
            final_result.push_back(temp);
        }
    }

    if(final_result.empty()){
        return "/";
    }else{
        string result;
        for(const auto & element:final_result)
            result += "/" + element;
        return result;
    }
}


int main(){
    string path = "/a/./b/../../c/";
    cout << simplifyPath(path) << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值