leetcode — simplify-path
import java.util.Stack;
/**
*
* Source : https://oj.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".
*
*/
public class SimplifyPath {
/**
* 简化unix形式的文件路径
* 考虑几种特殊情况
* 有多个斜杠
* 只剩下根路径的情况
* 路径中文件夹名称包含.
*
* @param path
* @return
*/
public String simplify (String path) {
Stack<String> stack = new Stack<String>();
stack.push("/");
String[] paths = path.split("/");
for (int i = 0; i < paths.length; i++) {
String cur = paths[i];
if (cur.equals("/") || cur.equals(".") || cur.equals("")) {
continue;
}
if (cur.equals("..")) {
if (stack.size() > 1) {
stack.pop();
}
} else {
stack.push(cur);
}
}
if (stack.size() == 0) {
return "/";
}
String result = "";
for (int i = 0; i < stack.size(); i++) {
result += stack.get(i);
if (stack.get(i).equals("/")) {
continue;
}
result += "/";
}
if (result.length() == 1) {
return result;
}
return result.substring(0, result.length() - 1);
}
public static void main(String[] args) {
SimplifyPath simplifyPath = new SimplifyPath();
System.out.println(simplifyPath.simplify("/home/"));
System.out.println(simplifyPath.simplify("/a/./b/../../c/"));
System.out.println(simplifyPath.simplify("/../"));
System.out.println(simplifyPath.simplify("/../a/b"));
System.out.println(simplifyPath.simplify("/home//foo/"));
System.out.println(simplifyPath.simplify("/home///foo/"));
System.out.println(simplifyPath.simplify("/home/foo.bar/"));
System.out.println(simplifyPath.simplify("/home/.bar/"));
}
}
leetcode — simplify-path的更多相关文章
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [leetcode]Simplify Path @ Python
原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...
- Leetcode 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/", ...
- [LeetCode] Simplify Path,文件路径简化,用栈来做
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- python3中报错:TypeError: 'range' object doesn't support item deletion
1.源代码 以下代码执行时会报 range' object does not support item assignment 的错误,问题出现在第17行的runge(10): import unit ...
- mysql查询前一天的数据
curdate()表示当天日期 统计前一天的日志sql语句: day); 要求: 统计从昨天开始统计前7天的日志包括昨天 day) --------------------- date_sub( ...
- POI导入和导出Excel总结
POI导入和导出Excel总结 POI使用总结 1.POI读取Excel 打开工作簿的方式有以下两种简单的应用,POI读取和输出工作簿文件都可以通过以下两种方式来声明: //通过输入流的方式打开本 ...
- Document.write和 getElementById(ID)
在javascript中,document.write()方法:常用来网页向文档中输出内容. 示例:通过document.write()方法,向网页文档中输出了一段文字. document.write ...
- leetcode - valid number 正则表达式解法
import java.util.regex.Pattern; public class Solution { Pattern p = Pattern.compile("^[\\+\\-]? ...
- Layui++>>ajax传递数组,防止深度序列化
- nginx三种安装方法(转载)
Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.它最常的用途是提供反向代理服务. 1.安装包编译安装 2.yum源安装 3.使用 ...
- Hadoop集群及基本组件搭建
本人采用一个master和两个slave的网络结构,具体搭建如下 1.准备安装包 1.下载安装包 http://pan.baidu.com/s/1jIoZulw 2.安装包清单 scala-2.12. ...
- Linux pwn入门教程(10)——针对函数重定位流程的几种攻击
作者:Tangerine@SAINTSEC 本系列的最后一篇 感谢各位看客的支持 感谢原作者的付出一直以来都有读者向笔者咨询教程系列问题,奈何该系列并非笔者所写[笔者仅为代发]且笔者功底薄弱,故无法解 ...
- 字符编码那点事:快速理解ASCII、Unicode、GBK和UTF-8
原作者:阮一峰(ruanyifeng.com),现重新整理发布,感谢原作者的无私分享. 1.引言 今天中午,我突然想搞清楚 Unicode 和 UTF-8 之间的关系,就开始查资料. 这个问题比我想象 ...