Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

题目大意:给一个String,表示一个文件的绝对路径,简化它。

解题思路:用一个栈表示即可,..向上返回一层,就是出栈一个元素,.不做操作,其他的入栈。

  1. public class Solution {
  2. public String simplifyPath(String path) {
  3. if (path == null || path.length() == 0) {
  4. return null;
  5. }
  6. Deque<String> deque = new ArrayDeque<>();
  7. String[] dirs = path.split("/");
  8. for (String dir : dirs) {
  9. if (".".equals(dir)){
  10. continue;
  11. }
  12. else if ("..".equals(dir)) {
  13. if (!deque.isEmpty()) {
  14. deque.pop();
  15. }
  16. } else {
  17. if(dir==null||dir.length()==0){
  18. continue;
  19. }
  20. deque.push(dir);
  21. }
  22. }
  23. String res = "/";
  24. while (!deque.isEmpty()) {
  25. res += deque.pollLast();
  26. res += "/";
  27. }
  28. res = res.substring(0, res.length() > 1 ? res.length() - 1 : 1);
  29. return res;
  30. }
  31. }

Simplify Path——LeetCode的更多相关文章

  1. Simplify Path [LeetCode]

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

  2. Simplify Path leetcode java

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

  3. leetcode面试准备:Simplify Path

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

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

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

  5. 【LeetCode】71. Simplify Path

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

  6. [LintCode] Simplify Path 简化路径

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

  7. 56. Edit Distance && Simplify Path

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

  8. 71. Simplify Path(M)

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

  9. [LeetCode] Simplify Path 简化路径

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

随机推荐

  1. StructureMap Exception Code: 207 Internal exception while creating Instance '06fc8bd7-76db-47c1-8d71-31090a074f5e' of PluginType QIMS.Repository.IComStaffRepository. Check the inner exception for more

    标题翻译: StructureMap异常代码:207内部异常,同时创造PluginType QIMS.Repository.IComStaffRepository的实例“06fc8bd7-76db-4 ...

  2. HTML特效代码大全

    1)贴图:<img src="图片地址">2)加入连接:<a href="所要连接的相关地址">写上你想写的字</a>1)贴 ...

  3. 21、javascript 基础

    Javascript Javascript 是一种嵌入到HTML文档的脚本语言,由浏览器解释和执行,无需编译. Javascript 是大小写敏感的,大写字母与小写字母不同. 使用“:”分号来分割每一 ...

  4. 一条sql语句循环插入N条不同记录(转)

    SET NOCOUNT ON IF (OBJECT_ID('TB' ) IS NOT NULL ) DROP TABLE TB GO CREATE TABLE TB(ID INT IDENTITY ( ...

  5. jQuery实现页面元素智能定位

    实现过程 Js侦听滚动事件,当页面滚动的距离(页面滚动的高度)超出了对象(要滚动的层)距离页面顶部的高度,即要滚动的层到达了浏览器窗口上边缘时,立即将对象定位属性position值改成fixed(固定 ...

  6. Java学习----对象间的继承

    继承:子类可以使用父类非私有的成员变量和方法 public class Father { public String name; public String bloodType; private in ...

  7. jquery中eq和get的区别与使用方法

    $("p").eq(0).css("color") //因为eq(num)返回的是个jq对象,所以可以用jq的方法css使用get来获得第一个p标签的color ...

  8. Dev gridview 调整字体大小

    //调整表头字体大小 this.gridView1.Appearance.HeaderPanel.Font = new Font("Tahoma", 20, FontStyle.R ...

  9. Ubuntu 12.04 下安装配置 JDK 7(tar)

    第一步:下载jdk-7u45-linux-i586.tar.gz 到Orcale的JDK官网下载JDK7的tar包 第二步:解压安装 tar -zxvf ./jdk-7u45-linux-i586.t ...

  10. PHP面向对象(OOP):__toString()方法

    我们前面说过在类里面声明“__”开始的方法名的方法(PHP给我们提供的),都是在某一时刻不同情况下自动调用执行的方 法,“__toString()”方法也是一样自动被调用的,是在直接输出对象引用时自动 ...