String - 71. Simplify Path
时间:2022-07-25
本文章向大家介绍String - 71. Simplify Path,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
- 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 UNIX-style file system, a period .
refers to the current directory. Furthermore, a double period ..
moves the directory up a level.
Note that the returned canonical path must always begin with a slash /
, and there must be only a single slash /
between two directory names. The last directory name (if it exists) must not end with a trailing /
. Also, the canonical path must be the shortest string representing the absolute path.
Example 1:
Input: "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.
Example 2:
Input: "/../"
Output: "/"
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
Example 3:
Input: "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
Example 4:
Input: "/a/./b/../../c/"
Output: "/c"
思路:
题目意思简化一个unix系统下的路径。.
代表当前目录, ..
代表上一级目录,做法就是用栈来做,先把字符串根据 /
切割成字符串数组,然后遍历数组,如果当前字符串是 .
,就不管,继续下一个,如果是 ..
就把栈顶元素弹出,其他情况就入栈,最后在把栈中的元素用 /
拼接在一起就可以。
代码:
go:
func simplifyPath(path string) string {
if path == "" {
return path
}
var stack []string
pathArr := strings.Split(path, "/")
for _, p := range pathArr {
if p == "." || p == "" {
continue
} else if p == ".." {
if len(stack) != 0 {
stack = stack[:len(stack)-1]
}
} else {
stack = append(stack, p)
}
}
return "/" + strings.Join(stack, "/")
}
- 2017,人们视算法为洪水猛兽;算法说:我不想背锅
- 深入浅出事件流处理NEsper(三)
- 用Flex模拟智能手机表单输入的自动放大功能
- c#4.0中的动态编程
- 手把手教 Vue-环境搭建
- 马化腾:通向互联网未来的七个路标
- 微信小程序,让生活不一样
- rsync+inotify实时同步环境部署记录
- 常用rsync命令操作梳理
- 无人驾驶系列——深度学习笔记:Tensorflow基本概念
- Android Fragment应用实战
- c#4.0中的不变(invariant)、协变(covariant)、逆变(contravariant)小记
- 用于.NET的可移植HTTP客户端
- 我是如何利用CSRF Get DedeCms Shell的
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- laravel5.6实现数值转换
- python中return不返回值的问题解析
- php装饰者模式简单应用案例分析
- php常用日期时间函数实例小结
- PHP超级全局变量【$GLOBALS,$_SERVER,$_REQUEST等】用法实例分析
- 基于laravel缓冲cache的用法详解
- Python使用sys.exc_info()方法获取异常信息
- laravel通用化的CURD的实现
- Laravel修改验证提示信息为中文的示例
- PHP+redis实现微博的推模型案例分析
- Laravel 解决composer相关操作提示php相关异常的问题
- laravel 实现根据字段不同值做不同查询
- php实现获取近几日、月时间示例
- PHP+redis实现微博的拉模型案例详解
- PHP实现微信申请退款功能