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 ...
随机推荐
- T-2-java面向对象
一.类 类对象的数据结构定义,方法是对象的行为. 类是数据类型. 一个类可以创建多个对象,这多个对象结构相同,数据不同. 类中可以包含:(1)成员变量(对象的共同特征,静的):(2)方法(对象的共同行 ...
- WindowsPE权威指南 第二章 小工具 PEComp代码的C语言实现
主程序代码 PEComp.c #include <windows.h> #include <Richedit.h> #include <Commctrl.h> #i ...
- 使用idea搭建maven-web项目
使用idea搭建maven-web项目 1.用idea搭建项目:File--new--project 2.选择jdk版本,选择Maven-archetype-webapp来创建maven-web项目如 ...
- 京东Alpha平台开发笔记系列(二)
第一篇博文简单讲了一下京东Alpha平台与个人idea技能,本篇将讲解Alpha平台与个人开发需要的一些知识,下面开篇 ——>>> 上图就是京东Alpha技能平台的首页,Skill平 ...
- cp/tar/用c语言编写程序 实现cp命令的效果
1.cp (拷贝) 已存在文件路径 要拷贝的文件路径 实现cp命令的代码如下: #include <stdio.h> //因为要在命令中得到两个路径,所以要用到main函数的两个参数 i ...
- 算法学习笔记:knn理论介绍
阅读对象:了解指示函数,了解训练集.测试集的概念. 1.简介 knn算法是监督学习中分类方法的一种.所谓监督学习与非监督学习,是指训练数据是否有标注类别,若有则为监督学习,若否则为非监督学习.所谓K近 ...
- opencv2.4.13+python2.7学习笔记--OpenCV中的图像处理--图像轮廓
阅读对象:无要求. 1.代码 ''' OpenCV中的轮廓 轮廓可以简单认为成将连续的点(连着边界)连在一起的曲线,具有相同的颜色或者灰度.为了更加准确,要使用二值化图像.在寻找轮廓之前,要进行阈值化 ...
- java(二)Web部分
2.1.1讲一下http get和post请求的区别? GET和POST请求都是http的请求方式,用户通过不同的http的请求方式完成对资源(url)的不同操作.GET,POST,PUT,DELET ...
- ubuntu+apache2设置访问、重定向到https
环境:ubunt14裸机,apache2,php5 条件:证书(部分商家买域名送一年),域名,为了方便均在root用户下进行的 web目录:/var/www/test 证书目录(自建):/etc/ap ...
- 关于Selenium WebDriver的geckodriver
下载Selenium的最新版本地址:http://selenium-release.storage.googleapis.com/index.html 友情提示:如果一直下载不了,可能是浏览器与下载工 ...