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

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

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

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

public class Solution {
public String simplifyPath(String path) {
if (path == null || path.length() == 0) {
return null;
}
Deque<String> deque = new ArrayDeque<>();
String[] dirs = path.split("/");
for (String dir : dirs) {
if (".".equals(dir)){
continue;
}
else if ("..".equals(dir)) {
if (!deque.isEmpty()) {
deque.pop();
}
} else {
if(dir==null||dir.length()==0){
continue;
}
deque.push(dir);
}
}
String res = "/";
while (!deque.isEmpty()) {
res += deque.pollLast();
res += "/";
}
res = res.substring(0, res.length() > 1 ? res.length() - 1 : 1);
return res;
}
}

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. linux的grep命令

    参考文档如下: linux grep命令 grep abb15455baeb4b23ab47540272ec47eb epps-sas.log | grep operateSettleBill exp ...

  2. 通常我们使用[NSDate date]方法得到的时间与当前时间不一致,如何解决?

    NSDate *date = [NSDate date];    NSTimeZone *zone = [NSTimeZone systemTimeZone];    NSInteger interv ...

  3. Base64的用法

    如果要对一些图片进行保存,把他的文件名变成乱码,不让用户把他删掉,可以用Base64把他删掉用法如下: //获取访问图片的路径 String path=editText.getText().toStr ...

  4. JSP标准标签库的安装以及自定义标签的创建

    JSTL 库安装 Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip). 官方下载 ...

  5. spring data jpa Specification 例子

    /** * 封装查询条件 * * @param baseQueryDTO * @return */ private Specification<ActivityBase> getSpeci ...

  6. 封装Timer

    System.Timers.Timer,System.Timers.Timer在使用的过程中需要: 1.构造函数不同,构造函数可以什么事情也不做,也可以传入响应间隔时间:System.Timers.T ...

  7. OS X Yosemite下安装Hadoop2.5.1伪分布式环境

    最近开始学习Hadoop,一直使用的是公司配好的环境.用了一段时间后发现对Hadoop还是一知半解,故决定动手在本机上安装一个供学习研究使用.正好自己用的是mac,所以没啥说的,直接安装. 总体流程 ...

  8. bootstrap学习--模态弹出框modals轮子

    1.点击按钮型 <link rel="stylesheet" href="lib/bootstrap/css/bootstrap.min.css"> ...

  9. 通过命令修改wampserver的mysql密码

    WAMP安装好后,mysql教程密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按 ...

  10. bootstrap日期时间插件datetimepicker

    <!DOCTYPE HTML> 02 <html> 03   <head> 04     <link href="http://netdna.boo ...