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中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp

Java中Switch Case多个条件处理方法举例

《Java中SwitchCase多个条件处理方法举例》Java中switch语句用于根据变量值执行不同代码块,适用于多个条件的处理,:本文主要介绍Java中SwitchCase多个条件处理的相... 目录前言基本语法处理多个条件示例1:合并相同代码的多个case示例2:通过字符串合并多个case进阶用法使用