当前位置: 首页 > news >正文

待解决 leetcode71 简化路径 栈的应用

用多种ifelse很不好很复杂容易丢情况

class Solution {
public:
    string simplifyPath(string path) {
        stack<char> st;
        string result;
        int n = path.size();
        
        while(n > 1 && (path[n-1] == '/' || path[n-1] == '.')){
            if(n > 2 && path[n-2] == '.' && path[n-1] == '.' ){
                break;
            }
            if(n > 2 && path[n-2] != '/' && path[n-1] == '.' ){
                break;
            }
            n -- ;
            
        }
        for(int i = 0; i <= n-1; i++){
            while(!st.empty() && st.top() == '/' && path[i] == '/'){
                i++;
            }
            if(!st.empty() && st.top() == '/' && path[i] == '.' && (i + 1 >= n || path[i+1] =='/')){
                i++;
                continue;
            }else if(!st.empty() && st.top() == '/' && path[i] == '.' && i+1 < n && path[i+1] == '.' && (i + 2 >= n || path[i+2] == '/')){
                i += 2;
                if(!st.empty()){
                    st.pop();
                    if(st.empty()){
                        st.push('/');
                    }
                    while(!st.empty() && st.top() != '/'){
                        st.pop();
                    }
                }
                continue;
            }else{
                st.push(path[i]);
            }
        }

        if (!st.empty() && st.top() == '/' && st.size() > 1) {
            st.pop();
        }
        
        for(int i = st.size();i>0;i--){
            result = st.top() + result;
            st.pop();
        }
        return result;
    }
};

另一种方法等一等后面加入吧

相关文章:

  • 【深入探索 Caffeine:Java 缓存利器】
  • 0.MySQL安装|卸载内置环境|配置官方yum源|安装mysql|登录mysql|设置配置文件(centos8.2)
  • docker 部署 rocketmq
  • MySQL在线、离线安装
  • 计算机毕业设计SpringBoot+Vue.js社区医院信息平台(源码+文档+PPT+讲解)
  • Nginx安装并配置https
  • RTSP/Onvif安防平台EasyNVR接入EasyNVS显示服务缺失的原因与解决方案
  • 阿里云ack的创建与实战应用案例
  • 音乐游戏Pump It Up(PIU)模拟器
  • Pytorch使用手册--将 PyTorch 模型导出为 ONNX(专题二十六)
  • 日本IT|Python开发工程师的职业前景
  • 第一 二章 小车硬件介绍-(全网最详细)基于STM32智能小车-蓝牙遥控、避障、循迹、跟随、PID速度控制、视觉循迹、openmv与STM32通信、openmv图像处理、smt32f103c8t6
  • 航旅纵横测试开发一面面经
  • Spring基础01
  • DOM 事件 HTML 标签属性速查手册
  • Remainder Problem CF1207F
  • APNG格式图片文件大小优化方案 转WEBP
  • 品牌全链路电商代运营:行业格局与头部服务商解析
  • 数据存储:一文掌握存储数据到ElasticSearch详解
  • 【监督学习】ARIMA预测模型步骤及matlab实现
  • 哔哩哔哩网页版在线观看网址/搜索引擎营销优化策略有哪些
  • 品牌建设网站公司/网络运营培训班多少钱
  • 软件开发公司简介模板/成都seo顾问
  • 宣传册样式/seo优化公司排名
  • 嘉善网站建设/免费网站申请域名
  • 网站设计与开发专业/微信指数是搜索量吗