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

相关文章

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

C#从XmlDocument提取完整字符串的方法

《C#从XmlDocument提取完整字符串的方法》文章介绍了两种生成格式化XML字符串的方法,方法一使用`XmlDocument`的`OuterXml`属性,但输出的XML字符串不带格式,可读性差,... 方法1:通过XMLDocument的OuterXml属性,见XmlDocument类该方法获得的xm

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed

Redis如何使用zset处理排行榜和计数问题

《Redis如何使用zset处理排行榜和计数问题》Redis的ZSET数据结构非常适合处理排行榜和计数问题,它可以在高并发的点赞业务中高效地管理点赞的排名,并且由于ZSET的排序特性,可以轻松实现根据... 目录Redis使用zset处理排行榜和计数业务逻辑ZSET 数据结构优化高并发的点赞操作ZSET 结

微服务架构之使用RabbitMQ进行异步处理方式

《微服务架构之使用RabbitMQ进行异步处理方式》本文介绍了RabbitMQ的基本概念、异步调用处理逻辑、RabbitMQ的基本使用方法以及在SpringBoot项目中使用RabbitMQ解决高并发... 目录一.什么是RabbitMQ?二.异步调用处理逻辑:三.RabbitMQ的基本使用1.安装2.架构

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置