LintCode 421 Simplify Path (字符串处理题)

2023-11-24 11:01

本文主要是介绍LintCode 421 Simplify Path (字符串处理题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

421 · Simplify Path
Algorithms

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

In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period … moves the directory up a level.

The result must always begin with /, and there must be only a single / between two directory names. The last directory name (if it exists) must not end with a trailing /. Also, the result must be the shortest string representing the absolute path.

Did you consider the case where path is “/…/”?

In this case, you should return “/”.

Another corner case is the path might contain multiple slashes ‘/’ together, such as “/home//foo/”.

In this case, you should ignore redundant slashes and return “/home/foo”.

Example
Example 1:

Input: “/home/”
Output: “/home”
Example 2:

Input: “/a/./…/…/c/”
Output: “/c”
Explanation: “/” has no parent directory, so “/…/” equals “/”.
Tags
Company
OpenAI
Facebook
Microsoft

class Solution {
public:/*** @param path: the original path* @return: the simplified path*/string simplifyPath(string &path) {int n = path.size();string res = "";int pos = 0, dirStartPos = 0, dirEndPos = 0;stack<string> stk;while (pos < n) {while (pos < n && path[pos] == '/') pos++;dirStartPos = pos;while (pos < n && path[pos] != '/') pos++;dirEndPos = pos;string dirStr = path.substr(dirStartPos, dirEndPos - dirStartPos);if (dirStr == "..") {if (!stk.empty()) stk.pop();} else if (dirStr.size() > 0 && dirStr != ".") {stk.push(dirStr);}}while (!stk.empty()) {res = "/" + stk.top() + res;stk.pop(); }if (res.size() == 0) res = "/";return res;}
};

不用stack用vector也可以。

class Solution {
public:/*** @param path: the original path* @return: the simplified path*/string simplifyPath(string &path) {vector<string> dirs;int pos = 0;while (pos < path.size()) {while (path[pos] == '/' && pos < path.size()) ++pos;if (pos == path.size()) break;int startPos = pos;while (path[pos] != '/' && pos < path.size()) ++pos;int endPos = pos - 1;string s = path.substr(startPos, endPos - startPos + 1);if (s == "..") {if (!dirs.empty()) dirs.pop_back();} else if (s != ".") {dirs.push_back(s);}}if (dirs.empty()) return "/";string result;for (int i = 0; i < dirs.size(); ++i) {result += '/' + dirs[i];}return result;}
};

这篇关于LintCode 421 Simplify Path (字符串处理题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/421817

相关文章

电脑提示xlstat4.dll丢失怎么修复? xlstat4.dll文件丢失处理办法

《电脑提示xlstat4.dll丢失怎么修复?xlstat4.dll文件丢失处理办法》长时间使用电脑,大家多少都会遇到类似dll文件丢失的情况,不过,解决这一问题其实并不复杂,下面我们就来看看xls... 在Windows操作系统中,xlstat4.dll是一个重要的动态链接库文件,通常用于支持各种应用程序

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

Golang 日志处理和正则处理的操作方法

《Golang日志处理和正则处理的操作方法》:本文主要介绍Golang日志处理和正则处理的操作方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录1、logx日志处理1.1、logx简介1.2、日志初始化与配置1.3、常用方法1.4、配合defer

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

Python处理大量Excel文件的十个技巧分享

《Python处理大量Excel文件的十个技巧分享》每天被大量Excel文件折磨的你看过来!这是一份Python程序员整理的实用技巧,不说废话,直接上干货,文章通过代码示例讲解的非常详细,需要的朋友可... 目录一、批量读取多个Excel文件二、选择性读取工作表和列三、自动调整格式和样式四、智能数据清洗五、

golang float和科学计数法转字符串的实现方式

《golangfloat和科学计数法转字符串的实现方式》:本文主要介绍golangfloat和科学计数法转字符串的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望... 目录golang float和科学计数法转字符串需要对float转字符串做处理总结golang float