LeetCode——Simplify Path
来源:互联网 发布:计算机与网络基础知识 编辑:程序博客网 时间:2024/04/30 00:46
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 case where path =
"/../"
?
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"
.
思路:
又是一个字符串处理题。
面对这种题目一定要保持清醒,先分析好题目之后再开始码代码。
题目的要求是输出Unix下的最简路径,Unix文件的根目录为"/","."表示当前目录,".."表示上级目录。
例如:
输入1:
/../a/b/c/./..
输出1:
/a/b
模拟整个过程:
1. "/" 根目录
2. ".." 跳转上级目录,上级目录为空,所以依旧处于 "/"
3. "a" 进入子目录a,目前处于 "/a"
4. "b" 进入子目录b,目前处于 "/a/b"
5. "c" 进入子目录c,目前处于 "/a/b/c"
6. "." 当前目录,不操作,仍处于 "/a/b/c"
7. ".." 返回上级目录,最终为 "/a/b"
我用一个堆栈来模拟路径的行为,遇到"."不操作,遇到".."退栈,其他情况都压入堆栈。
0 0
- LeetCode——Simplify Path
- LeetCode——Simplify Path
- leetcode 071 —— Simplify Path
- [LeetCode]—Simplify Path 简化路径表达式
- leetcode——71——Simplify Path
- LeetCode: Simplify Path
- LeetCode Simplify Path
- LeetCode : Simplify Path
- [Leetcode] Simplify Path
- [leetcode] simplify path
- [LeetCode] Simplify Path
- leetcode 100: Simplify Path
- [leetcode]Simplify Path
- [LeetCode]Simplify Path
- [leetCode] Simplify Path
- [leetcode]Simplify Path
- [leetcode] Simplify Path
- [LeetCode] Simplify Path
- JSON字符串与JSON对象的区别
- 怎么样让用户觉得产品更有价值?让他们DIY吧!
- python在win7 64位下几个小问题
- php变量范围之静态变量
- linux下由于线程局部存储未初始化导致加载动态链接库时程序崩溃的BUG
- LeetCode——Simplify Path
- Windows完成端口(IOCP)简介
- 使用存储过程 从数据库导出insert语句
- #个人学习记录#关于mpi_gather的接收顺序问题
- php常用字符串函数
- 带宽时延积、TCP 滑动窗口和拥塞窗口
- html/css知识lesson3
- activeMQ+stomp+php实现消息队列
- JavaNIO整理