1. import java.util.Stack;
  2. /**
  3. *
  4. * Source : https://oj.leetcode.com/problems/simplify-path/
  5. *
  6. *
  7. *
  8. * Given an absolute path for a file (Unix-style), simplify it.
  9. *
  10. * For example,
  11. * path = "/home/", => "/home"
  12. * path = "/a/./b/../../c/", => "/c"
  13. *
  14. *
  15. * Corner Cases:
  16. *
  17. * Did you consider the case where path = "/../"?
  18. * In this case, you should return "/".
  19. * Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
  20. * In this case, you should ignore redundant slashes and return "/home/foo".
  21. *
  22. */
  23. public class SimplifyPath {
  24. /**
  25. * 简化unix形式的文件路径
  26. * 考虑几种特殊情况
  27. * 有多个斜杠
  28. * 只剩下根路径的情况
  29. * 路径中文件夹名称包含.
  30. *
  31. * @param path
  32. * @return
  33. */
  34. public String simplify (String path) {
  35. Stack<String> stack = new Stack<String>();
  36. stack.push("/");
  37. String[] paths = path.split("/");
  38. for (int i = 0; i < paths.length; i++) {
  39. String cur = paths[i];
  40. if (cur.equals("/") || cur.equals(".") || cur.equals("")) {
  41. continue;
  42. }
  43. if (cur.equals("..")) {
  44. if (stack.size() > 1) {
  45. stack.pop();
  46. }
  47. } else {
  48. stack.push(cur);
  49. }
  50. }
  51. if (stack.size() == 0) {
  52. return "/";
  53. }
  54. String result = "";
  55. for (int i = 0; i < stack.size(); i++) {
  56. result += stack.get(i);
  57. if (stack.get(i).equals("/")) {
  58. continue;
  59. }
  60. result += "/";
  61. }
  62. if (result.length() == 1) {
  63. return result;
  64. }
  65. return result.substring(0, result.length() - 1);
  66. }
  67. public static void main(String[] args) {
  68. SimplifyPath simplifyPath = new SimplifyPath();
  69. System.out.println(simplifyPath.simplify("/home/"));
  70. System.out.println(simplifyPath.simplify("/a/./b/../../c/"));
  71. System.out.println(simplifyPath.simplify("/../"));
  72. System.out.println(simplifyPath.simplify("/../a/b"));
  73. System.out.println(simplifyPath.simplify("/home//foo/"));
  74. System.out.println(simplifyPath.simplify("/home///foo/"));
  75. System.out.println(simplifyPath.simplify("/home/foo.bar/"));
  76. System.out.println(simplifyPath.simplify("/home/.bar/"));
  77. }
  78. }

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. T-2-java面向对象

    一.类 类对象的数据结构定义,方法是对象的行为. 类是数据类型. 一个类可以创建多个对象,这多个对象结构相同,数据不同. 类中可以包含:(1)成员变量(对象的共同特征,静的):(2)方法(对象的共同行 ...

  2. WindowsPE权威指南 第二章 小工具 PEComp代码的C语言实现

    主程序代码 PEComp.c #include <windows.h> #include <Richedit.h> #include <Commctrl.h> #i ...

  3. 使用idea搭建maven-web项目

    使用idea搭建maven-web项目 1.用idea搭建项目:File--new--project 2.选择jdk版本,选择Maven-archetype-webapp来创建maven-web项目如 ...

  4. 京东Alpha平台开发笔记系列(二)

    第一篇博文简单讲了一下京东Alpha平台与个人idea技能,本篇将讲解Alpha平台与个人开发需要的一些知识,下面开篇 ——>>> 上图就是京东Alpha技能平台的首页,Skill平 ...

  5. cp/tar/用c语言编写程序 实现cp命令的效果

    1.cp (拷贝) 已存在文件路径  要拷贝的文件路径 实现cp命令的代码如下: #include <stdio.h> //因为要在命令中得到两个路径,所以要用到main函数的两个参数 i ...

  6. 算法学习笔记:knn理论介绍

    阅读对象:了解指示函数,了解训练集.测试集的概念. 1.简介 knn算法是监督学习中分类方法的一种.所谓监督学习与非监督学习,是指训练数据是否有标注类别,若有则为监督学习,若否则为非监督学习.所谓K近 ...

  7. opencv2.4.13+python2.7学习笔记--OpenCV中的图像处理--图像轮廓

    阅读对象:无要求. 1.代码 ''' OpenCV中的轮廓 轮廓可以简单认为成将连续的点(连着边界)连在一起的曲线,具有相同的颜色或者灰度.为了更加准确,要使用二值化图像.在寻找轮廓之前,要进行阈值化 ...

  8. java(二)Web部分

    2.1.1讲一下http get和post请求的区别? GET和POST请求都是http的请求方式,用户通过不同的http的请求方式完成对资源(url)的不同操作.GET,POST,PUT,DELET ...

  9. ubuntu+apache2设置访问、重定向到https

    环境:ubunt14裸机,apache2,php5 条件:证书(部分商家买域名送一年),域名,为了方便均在root用户下进行的 web目录:/var/www/test 证书目录(自建):/etc/ap ...

  10. 关于Selenium WebDriver的geckodriver

    下载Selenium的最新版本地址:http://selenium-release.storage.googleapis.com/index.html 友情提示:如果一直下载不了,可能是浏览器与下载工 ...