当前位置: 首页 > 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;
    }
};

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

http://www.dtcms.com/a/40145.html

相关文章:

  • 【深入探索 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实现
  • 【Web世界探险家】CSS美学(一)
  • 【REST2SQL】15银河麒麟系统下达梦数据库部署REST2SQL
  • springboot015基于SpringBoot的在线视频教育平台的设计与实现(源码+数据库+文档)
  • 音频进阶学习十六——LTI系统的差分方程与频域分析一(频率响应)
  • 【实战】使用PCA可视化神经网络提取后的特征空间【附源码】
  • 通俗易懂:RustDesk Server的搭建及使用
  • 《Android-RecyclerView实现封面滑动到指定位置放大》---ViewPager封面指示器
  • 从同步到异步:Vue 3 的异步更新策略与 `tick` 机制
  • 应对现代生活的健康养生指南
  • 【算法】798. 差分矩阵