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. 使用strut2要注意的问题

  2. 调优系列-tomcat调优

    http://www.360doc.com/content/14/1208/13/16070877_431273418.shtml 使用JMeter对Tomcat进行压力测试与Tomcat性能调优 n ...

  3. Win10安卓模拟器Visual Studio Emulator for Android使用简介(转)

    Visual Studio Emulator for Android是微软官方发布的独立版本的安卓模拟器,这款软件可以让安卓应用开发者更加轻松的用Visual Studio编写Android应用,据说 ...

  4. ab安装和使用

    apache bench(专门用于 HTTP Server .单url).win8: 下载地址:http://httpd.apache.org/download.cgi#apache24 安装apac ...

  5. ORACLE用户管理方式下备份数据和复制数据库

    首先要明确的是,oracle数据库的备份可以分为逻辑备份和物理备份.           逻辑备份的是通过数据导出对数据进行备份,主要方式有老式的IMP/EXP和数据泵灯方式.适合变化较少的数据库,而 ...

  6. http错误代码含义大全详解

    http 错误代码表 所有 HTTP 状态代码及其定义.  代码  指示  2xx  成功  200  正常:请求已完成.  201  正常:紧接 POST 命令.  202  正常:已接受用于处理, ...

  7. C#入门经典(第五版)学习笔记(二)

    ---------------函数---------------参数数组:可指定一个特定的参数,必须是最后一个参数,可使用个数不定的参数调用函数,用params关键字定义它们 例如: static i ...

  8. 安装SQLServer2008后Windows防火墙上的端口开放

    1.打开SQL Server 配置管理器-->SQL Server 网络配置-->XXX的协议,启用TCP/IP协议2.打开TCP/IP协议的属性,切至“IP地址”标签,拉至最下端的IPA ...

  9. 关于typedef的用法总结

    不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相似,但更多的是不同,特别是在一些复杂的用法上,就完全不同了,看了网上一些 ...

  10. Java多线程:Semaphore

    Semaphore为并发包中提供用于控制某资源同时可以被几个线程访问的类 实例代码: // 允许2个线程同时访问 final Semaphore semaphore = new Semaphore(2 ...