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. 4月23日 db 命令操作 和表操作

    1内容回顾: # 补充的知识点 # server端肯定是确定下来的 # mysql的客户端 # mysql.exe 直接在命令行就可以运行的 (学习阶段用) # navicat等可视化的客户端,是第三 ...

  2. 使用kbmmw 生成REST 服务OpenAPI函数原型

    我们以前介绍了很多kbmmw 开发REST 的例子.一直有个问题困惑着大家. 我们提供REST 服务,如何让客户端快速的使用,当然可以写文档,但是一旦 后台改变了,又要再一次给调用者发新文档,非常的麻 ...

  3. python之支付

    一,alipay方式 1,国内的alipay支付:我在网上找了好多的教程,大多数都是属于国内内支付的,所以在这里我就不详细介绍了, 操作:https://www.cnblogs.com/xuanan/ ...

  4. JavaScript深拷贝

    1,JSON.parse(JSON.stringify(obj)) 使用JSON实现深拷贝必须要求对象是符合JSON安全的,不了解JSON安全的自行百度. 2,lodash/underscore  _ ...

  5. ehcache如何配置

    1.pom.xml文件配置(主要针对jar包的引入) <ehcache.version>2.6.9</ehcache.version><ehcache-web.versi ...

  6. python模块:logging

    # Copyright 2001-2016 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and ...

  7. sftp修改用户home目录后登录时报connection closed by remote host

    在sftp用户需要修改登录根目录的情况下,我们可以修改/etc/ssh/sshd_config文件中ChrootDirectory /home/[path]的路径. 但是,在重启sshd服务后,sft ...

  8. Leetcode(三)无重复字符的最长子串

    3. 无重复字符的最长子串 题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最 ...

  9. Pi的计算

    百度百科           圆周率用希腊字母 π(读作pài)表示,是一个常数(约等于3.141592654),是代表圆周长和直径的比值.它是一个无理数,即无限不循环小数.在日常生活中,通常都用3. ...

  10. vue数据双向绑定

    Vue的双向绑定是通过数据劫持结合发布-订阅者模式实现的,即通过Object.defineProperty监听各个属性的setter,然后通知订阅者属性发生变化,触发相应的回调. 整个过程分为以下几步 ...