Simplify Path(路径简化)
问题:
来源:https://leetcode.com/problems/simplify-path
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".
思路:
先将路径按照“/”切分,然后遍历切分后的路径,利用栈的思想,遇到“”或者“.”就忽略,遇到“..”就出栈(除非栈中没有元素),遇到其他字符串就入栈,最后将栈中的字符串用“/”拼接起来
import java.util.Stack;
class Solution {
public String simplifyPath(String path) {
String[] subpaths = path.split("/");
Stack<String> stack = new Stack<String>();
for(String subpath: subpaths) {
if(subpath.equals(".") || subpath.equals("")) {
continue;
} else if(subpath.equals("..")) {
if(!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(subpath);
}
}
StringBuilder stringBuilder = new StringBuilder();
for(String subpath: stack) {
stringBuilder.append("/" + subpath);
}
if(stringBuilder.length() == 0) {
stringBuilder.append("/");
}
return stringBuilder.toString();
}
}
Simplify Path(路径简化)的更多相关文章
- LeetCode OJ:Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [Swift]LeetCode71. 简化路径 | Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LeetCode] Simplify Path,文件路径简化,用栈来做
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- lintcode 中等题:Simplify Path 简化路径
题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...
- 071 Simplify Path 简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...
- Leetcode71. Simplify Path简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
- Simplify Path leetcode java
题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&qu ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
随机推荐
- ArrayList为什么是线程不安全的
首先需要了解什么是线程安全:线程安全就是说多线程访问同一代码(对象.变量等),不会产生不确定的结果. 既然说ArrayList是线程不安全的,那么在多线程中操作一个ArrayList对象,则会出现不确 ...
- DB2常用指令
1. 启动实例(db2inst1): db2start 2. 停止实例(db2inst1): db2stop 3. 列出所有实例(db2inst1) db2ilist 3-1.列出当前实例: db2 ...
- 【技术分享:python 应用之二】解锁用 VSCode 写 python 的正确姿势
之前一直用 notepad++ 作为编辑器,偶然发现了 VScode 便被它的颜值吸引.用过之后发现它启动快速,插件丰富,下载安装后几乎不用怎么配置就可以直接使用,而且还支持 markdown.当然, ...
- 满减 HRBUST - 2455
https://vjudge.net/problem/HRBUST-2455 有两种优惠方式,一是满400减100,另外一种是商品自带折扣,二者不可叠加 dp[i][j]表示前i种商品,(参与满400 ...
- mysql 时间差
SELECT TIMESTAMPDIFF(minute,'2019-01-01 00:00:00', '2019-01-01 01:30:00') 返回结果 90 类推,有 SELECT TIMEST ...
- 【每日一包0006】dedupe
github地址:https://github.com/ABCDdouyae... dedupe 对数组进行去重,也可以自定义去重(比如要求数组的每一个对象的某个属性不重复) 文档地址:https:/ ...
- Route53 health check与 Cloudwatch alarm 没法绑定
原因 即使在控制台创建 创建的alarm会在us-east-1 不会再其他区域,目前route53 metric 在其他区域不存在. 所以使用cloudformation 创建 route53 hea ...
- @清晰掉 spi协议及工作原理分析
说明.文章摘自:SPI协议及其工作原理浅析 http://bbs.chinaunix.net/thread-1916003-1-1.html 一.概述. SPI, Serial Perripheral ...
- 【Spark机器学习速成宝典】基础篇02RDD常见的操作(Python版)
目录 引例入门:textFile.collect.filter.first.persist.count 创建RDD的方式:parallelize.textFile 转化操作:map.filter.fl ...
- Fatal error: Class 'think\db' not found
在model层写了一个查询语句结果报错 Fatal error: Class 'think\db' not found $list= Db::table('m_my_reserve_assess' ...