leetcode 71 Simplify Path

题目连接

https://leetcode.com/problems/simplify-path/ 

Simplify Path

Description

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

简化linux文件路径,”.”代表当前目录,”..”代表上级目录 
大体做法:先把path按’/’符分割保存起来(由于c++ string没有split函数,自己写一个吧) 
再把切割后的字符串扫一遍即可。 
具体如下

class Solution {
public:
    string simplifyPath(string path) {
        string ans;
        if(path.empty()) return ans;
        auto split = [=](char sep = '/')->vector<string> {
            string temp;
            vector<string> res;
            size_t start = 0, end = 0;
            while((end = path.find(sep, start)) != string::npos) {
                temp = path.substr(start, end - start);
                if(temp != "") res.emplace_back(temp);
                start = end + 1;
            }
            temp = path.substr(start);
            if(temp != "") res.emplace_back(temp);
            return res;
        };
        vector<string> res = split();
        vector<string> stk;
        for(auto &r: res) {
            if(r ==".") continue;
            else if(r == "..") {
                if(!stk.empty()) stk.pop_back();
            } else {
                stk.push_back(r);
            }
        }
        if(stk.empty()) {
            ans.push_back('/');
        } else {
            for(auto &r: stk) {
                ans.push_back('/');
                ans += r;
            }
        }
        return ans;
    }
};

 

转载于:https://www.cnblogs.com/GadyPu/p/5599045.html

基于Java+SpringBoot+微信小程序实现奶茶点单系统 再不用担心逢年过节的第一杯奶茶有没有人送了,制作一个奶茶点单系统自己当店主,开怀畅饮,一劳永逸。 阅读详情

相关推荐

三极管自激升压电路设计 - 从参数计算到实践验证

本文详细解析了三极管自激升压电路(焦耳小偷电路)的设计原理与实践方法。通过剖析自激振荡机制与三极管电流关系,指导读者完成从核心参数计算、关键元件选型到面包板搭建调试的全过程。文章重点阐述了如何利用电感、三极管等基础元件实现低压直流到高压的转换,是理解开关电源与电磁能量转换的绝佳实践项目。

weixin_29250573的博客 364

LeetCode刷题:71. Simplify Path

LeetCode刷题:71. Simplify Path 原题链接:https://leetcode.com/problems/simplify-path/ Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 308

ROS2实战:手把手教你用CMake集成自定义DDS(以CycloneDDS为例)

本文详细介绍了如何在ROS2中通过CMake集成自定义DDS(以CycloneDDS为例),涵盖环境准备、源码编译、ROS2工作区集成、性能调优及常见问题排查。通过RMW中间件抽象层,开发者可以灵活切换DDS实现,提升系统实时性和可靠性。

weixin_29284201的博客 347

【LeetCode】71. Simplify Path 解题报告(Python)

【LeetCode】71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/simplify-path/descr...

负雪明烛 2812

流式数据湖存储技术,Apache Paimon是什么?

是一项流式数据湖存储技术,可以为用户提供高吞吐、低延迟的数据摄入、流式订阅以及实时查询能力。Paimon 采用开放的数据格式和技术理念,可以与 Apache Flink / Spark / Trino 等诸多业界主流计算引擎进行对接,共同推进 Streaming Lakehouse 架构的普及和发展。

一个写湿的程序猿 2万+

Chromatix 7实战:从零开始调优ISP模块的完整流程(附常见报错解决方案)

本文提供了高通平台Camera调试工具Chromatix 7的完整实战指南。详细阐述了从环境准备、项目初始化、基础校正(如LSC、BPC)到核心ISP管线(Demosaic、CCM、HDR)及后处理(CAC、ANR)的调优全流程,并附带了常见报错(如“未初始化实例”)的解决方案,旨在帮助工程师建立系统性的调试方法论。

weixin_29290863的博客 620

LeetCode71——Simplify Path

LeetCode71——Simplify Path 依然是恶心的字符串处理,倒不是有多难,是你不知道他有哪些Sample Input,你得把所有的情况考虑进去。。。 我在做的时候就没有考虑非法输入,也就是说,我默认认为给定的输入都是合法的路径名。。。 但是题目会给出/home///near/ 这种非法路径输入,或者是 /home/zhangxiao/.../ 这种,非常恶心。。。

上善若水 1028

深度篇——图像处理的一些方法(一) 关于 图像处理的三个层次 与 selective search

返回主目录 返回 图像处理的一些方法 目录 下一章:深度篇——图像处理的一些方法(二)细说性能评估 IOU和 GIOU 目录内容 深度篇——图像处理的一些方法(一)关于图像处理的三个层次 与 selective search 深度篇——图像处理的一些方法(二)细说性能评估 IOU和 GIOU 深度篇——图像处理的一些方法(三)细说 HOG特征与bag-o...

天下所弃,不过其纪。世间事,在人,在志。 2916

LeetCode 71. Simplify Path(栈)

题目来源:https://leetcode.com/problems/simplify-path/ 问题描述 71.Simplify Path Medium Given anabsolute pathfor a file (Unix-style), simplify it. Or in other words, convert it to thecanonical path. ...

da_kao_la的博客 185

雷达工程师面试题目

详细描述项目背景、目标、你的具体职责和贡献,分析在项目实施过程中遇到的主要技术难题及解决方案,总结项目取得的成果和经验教训。

道亦无名 738

【StreamSet】ETL之StreamSet之Pipelines的状态监听之WebHook钉钉篇

介绍下StreamSets的状态监听以及钉钉的webhook的互联,这位博友问的恰是时候,不久前我刚刚对接过钉钉的机器人,这次看到这个问题,对我来说,应该轻车熟路了,那就有了这篇博文,状态监听之钉钉篇。...

m0_54252387的博客 487

python写算法题:leetcode: 71. Simplify Path

class Solution(object): def simplifyPath(self, path): """ :type path: str :rtype: str """ paths=path.split('/') outpath=[] for p in paths: ...

194

军品研制过程-转阶段

介绍军品转阶段评审

Abel的博客 2万+

LeetCode 71. Simplify Path

LeetCode 71. Simplify Path 本博客转载自:https://blog.youkuaiyun.com/makuiyu/article/details/44497901 Solution1:没理解啥意思,只能抄别人的答案了 这道题的要求是简化一个Unix风格下的文件的绝对路径。 字符串处理,由于”..”是返回上级目录(如果是根目录则不处理),因此可以考虑用栈记录路径名,以便于处理。...

Allenlzcoder的博客 395

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Leetcode 71 Simplify Path

Leetcode 71 Simplify Path Approach: Using Stacks Stack<String> stack = new Stack<String>(); String[] components = path.split("/"); Stack<String> stack = new Stack<String>(); ...

weixin_43876597的博客 164

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LeetCode 71 Simplify Path

LeetCode 71 Simplify Path Problem Description: 简化输入的绝对路径 具体的题目信息: https://leetcode.com/problems/simplify-path/description/ Attention: (1)/home/..abvh简化后仍是/home/..abvh (2)/home/…简化后仍是/home/… ...

萤火虫啊飞呀飞 211

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode71 Simplify Path java题解

题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" 题解: 解题思路:这题是简化目录,每一级目录前面都有一个斜杠,我可以首先对斜杠进行分割,分割之后

u012249528的专栏 1970

LeetCode71 Simplify Path

LeetCode71 Simplify Path问题描述Given an absolute path for a file (Unix-style), simplify it.For example, path = “/home/”, =>“/home”path = “/a/./b/../../c/”, => “/c”简单分析这道题就是简化路径,看到这个路径我就想到了坑爹的windows系列路径分

yanqinghe123的专栏 324

【Leetcode】71. Simplify Path

题目地址: https://leetcode.com/problems/simplify-path/ 给定一个代表目录的字符串,返回其最终的真实目录。用栈来模拟整个过程即可。先将整个字符串按照"/“切割,这样切割所得就是所有的目录以及“.”本级目录和”…“上级目录。遇到”…"的时候,如果栈不空,就pop;遇到“.”的时候略过;除此之外说明是文件夹,就直接入栈。但最后输出目录的时候,要注意从栈底元素...

数学、算法爱好者的博客 237

leetcode No71. Simplify Path

Question: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" Corner Cases: Did you consider the cas

Dufre 1007

LeetCode 71. Simplify Path【Java】

题目描述 71. 简化路径 AC代码 class Solution { public String simplifyPath(String path) { path+='/'; String res="",s=""; for(char c:path.toCharArray()){ if(res.isEmpty()) res+=c; else if(c!='/') s+=c; els

NayelyA的博客 227

Leetcode71- Simplify Path

1. 题目: 给定一个Unix系统下的路径,简化这个路径。 如 /home/ ,简化后为 /home 如 /a/./b/../../c/ ,简化后为 /c 2. 思路: 这还是一个入栈出栈匹配问题 构造无效字符集合 = {"",".",".."} 将字符串split成字符串数组 遍历字符串数组{ 如果元素等于“..”且栈不为空 出栈一个元素 如果元素不

sheldonwong的专栏 264

LeetCode-71 Simplify Path

Description Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period . .moves the directory up a

秦智慧的博客 186

【LeetCode】71. Simplify Path

问题描述https://leetcode.com/problems/simplify-path/#/descriptionGiven an absolute path for a file (Unix-style), simplify it.For example path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c"Corne

zgljl2012的专栏 1037

【Leetcode】71 Simplify path 简化路径

Simplify path1. 题目——来自leetcode Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/home” path = “/a/./b/../../c/

LYZ 813

[leetcode] 71. Simplify Path

Description Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file system, a period . refers to the current directory. F...

Learning from the mistakes 210
上一篇: leetcode 10 Regular Expression Matching
下一篇: leetcode 18 4Sum
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值