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的更多相关文章

  1. [LeetCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  2. [leetcode]Simplify Path @ Python

    原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...

  3. Leetcode Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  4. [LeetCode] Simplify Path(可以不用看)

    Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...

  5. [LeetCode] Simplify Path,文件路径简化,用栈来做

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  6. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

  7. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 【LeetCode】71. Simplify Path

    Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...

  9. [LintCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...

  10. 56. Edit Distance && Simplify Path

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...

随机推荐

  1. Python从入门到精通之Fifth!

    字典  类名:dict   字典是无序的. 字典的创建  info = {"k1":{1:"abc",[1,2,3]},"k2":True} ...

  2. 用pandas库修改excel文件里的内容,并把excel文件格式存为csv格式,再将csv格式改为html格式

    假设有Excel文件data.xlsx,其中内容为: ID  age  height     sex  weight张三   1   39     181  female      85李四   2  ...

  3. Linux环境(Centos7)下部署.NetCore2.0的Web应用

    Web应用基于Windows环境下开发,然后部署到Linux 1.进入VS2017,点击新建->项目->.NetCore->ASP.NET Core Web应用程序,确定 2.选择W ...

  4. 数据结构C语言版-队列

    #include <stdlib.h> #include <stdio.h> #include <iostream> using namespace std; ty ...

  5. git使用之后悔药

    1.工作区的代码想撤销 背景:有时候编写了一大段代码之后,想要撤销更改(执行add操作之前), 命令:git checkout -- <file路径> 使用git checkout -- ...

  6. Python 协程 61

    什么是协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程的特点 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到 ...

  7. java开发师笔试面试每日12题(2)

    1.Volatile和Synchronized不同点 (1).volatile只能作用于变量,使用范围较小.synchronized可以用在变量.方法.类.同步代码块等,使用范围比较广. (2).vo ...

  8. 生成图形化html报告

    生成图形化html报告: 1.从cmd 进入执行测试文件 2.执行该命令:jmeter -n -t <test JMX file> -l <test log file> -e ...

  9. [转]Java工程师技术栈--成神之路

    一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http://www.jcp.org/en/jsr/detail?id=133http://if ...

  10. Exp8 Web基础 20154320 李超

    1.实验后回答问题 (1)什么是表单. 表单是一个包含表单元素的区域,表单元素是允许用户在表单中输入信息的元素,表单在网页中主要负责数据采集功能,一个表单有三个基本组成部分:表单标签.表单域.表单按钮 ...