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 ...
随机推荐
- 使用kbmmw中的随机数替换delphi 10.3 自带的随机数
我们在开发中经常会使用随机数模拟各种随机条件,例如生成唯一的密码和令牌. 在计算机中,一般采用PRNG(伪随机序列发生器)模拟真实随机数.既然是随机,就要要没有任何规律, 在取值范围内均匀.独立.以确 ...
- spring的一个小例子(二)--解析前面的小例子
接上篇:http://www.cnblogs.com/xuejupo/p/5236448.html 首先应该明白,一个web项目,web.xml是入口. 然后下面来分析上篇博客中出现的web.xml: ...
- VS2017 Debug断点后显示UTF8字符串
断点后跟踪字幕文件文本,因为国内字幕一般是UTF8的,VS默认显示不出来,在变量上双击,加入 ,s8就可以了 默认 修改后 其他 ,数字 将变量拆分为数组显示, 数字是要显示多少位, 此法对cons ...
- shell编写自动化安装dhcp服务
#!/bin/bash#Auth:Darius#自动化安装dhcp服务#"$1"为测试IP,用来查看IP段是否能通eno=`ifconfig|awk '{print $1}'|he ...
- 【转载】 .NET框架设计—常被忽视的C#设计技巧
阅读目录: 1.开篇介绍 2.尽量使用Lambda匿名函数调用代替反射调用(走进声明式设计) 3.被忽视的特性(Attribute)设计方式 4.扩展方法让你的对象如虎添翼(要学会使用扩展方法的设计思 ...
- jenkins as code 与go语言学习
前言 最近看jenkins as code这个概念在很多文章中提起,持续交付中八大原则也有把一切都放入版本管理,最近准备把我们公司用的一些jenkins上的job的配置也放到git中,由于https: ...
- STM32 HAL库的使用心得
1.I2C函数中HAL_I2C_Mem_Write和HAL_I2C_Master_Transmit有啥区别?{ 使用HAL_I2C_Mem_Write等于 先使用HAL_I2C_Master_Tran ...
- 细说SpringDI Setter注入
依赖注入(DI)是一个过程,通过这个过程,对象可以通过构造函数参数,工厂方法的参数或者在构造或返回对象实例后设置的属性来定义它们的依赖关系从工厂方法.然后容器在创建bean时注入这些依赖关系.这个过程 ...
- Java 多线程之悲观锁与乐观锁
一.悲观锁 总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁(共享资源每次只给一个线程使用,其它线程阻塞,用完后再把资源 ...
- mysql命令行创建表,插入表数据
create table t_hero( id int unsigned auto_increment primary key, name varchar(10) unique not null, a ...