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

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

解题思路:

首先要了解linux目录的意思,请参考Linux的文件管理

先把path按" /" split,建立一个栈,如果出现..则出栈,否则入栈,最后输出这个栈即可,JAVA实现如下:

    public String simplifyPath(String path) {
Stack<String> res = new Stack<String>();
String[] ps = path.split("/");
for (String a : ps) {
if (a.equals("..")) {
if (!res.empty())
res.pop();
}
else if (a.equals(".") || a.equals(""))
continue;
else
res.push(a);
}
if (res.empty())
return "/";
StringBuilder sb = new StringBuilder();
while(!res.empty())
sb.append("/" + res.get(i));
return sb.toString();
}

Java for LeetCode 071 Simplify Path的更多相关文章

  1. [LeetCode] 71. Simplify Path 简化路径

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

  2. Leetcode 之Simplify Path @ python

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

  3. 071 Simplify Path 简化路径

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...

  4. 【leetcode】Simplify Path

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

  5. Java for LeetCode 064 Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  6. Leetcode#71 Simplify Path

    原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...

  7. leetcode[70] Simplify Path

    题目的意思是简化一个unix系统的路径.例如: path = "/home/", => "/home"path = "/a/./b/../../ ...

  8. Leetcode 之Simplify Path(36)

    主要看//之间的内容:如果是仍是/,或者是.,则忽略:如果是..,则弹出:否则压入堆栈.最后根据堆栈的内容进行输出. string simplifyPath(string const& pat ...

  9. leetcode面试准备:Simplify Path

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

随机推荐

  1. linux 问答

    问:1 如何查看当前的Linux服务器的运行级别? 答: ‘who -r’ 和 ‘runlevel’ 命令可以用来查看当前的Linux服务器的运行级别. 问:2 如何查看Linux的默认网关? 答: ...

  2. 【poj1144】 Network

    http://poj.org/problem?id=1144 (题目链接) 题意 求无向图的割点. Solution Tarjan求割点裸题.并不知道这道题的输入是什么意思,也不知道有什么意义= =, ...

  3. bzoj3037 创世纪

    两种解法: 一.树状DP /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring&g ...

  4. POJ1745Divisibility(01背包思想)

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11151   Accepted: 3993 Des ...

  5. Bootstrap Modals(模态框)

    http://www.runoob.com/bootstrap/bootstrap-v2-modal-plugin.html 描述 Bootstrap Modals(模态框)是使用定制的 Jquery ...

  6. ios 关键字 IB_DESIGNABLE IBInspectable 尝鲜

    每次都用代码定义一个属性,然后在viewDidLoad中再去设置这个属性,最常见的就是什么圆角,描边的, 现在终于可以直接像系统的属性一样在界面上设定了 界面上修改你的属性,减少代码压力

  7. javascript正则——贪婪匹配

    熟悉正则的朋友都知道,正则的匹配有“贪婪”和“非贪婪”之分. “贪婪”匹配是尽可能多的匹配: 对于字符串‘aaaa’, /a+/匹配整个字符串,而非贪婪匹配/a+?/匹配的是整个字符串的第一个‘a’, ...

  8. Hadoop之伪分布环境搭建

    搭建伪分布环境 上传hadoop2.7.0编译后的包并解压到/zzy目录下 mkdir /zzy 解压 tar -zxvf hadoop.2.7.0.tar.gz -C /zzy     配置hado ...

  9. [Effective JavaScript 笔记] 第6条:了解分号插入的局限

    分号可以省略 js可以在语句结束不强制加分号.(建议还是添加,不添加分号往往会出现不易发现的BUG) function Point(x,y){ this.x=x||0; this.y=y||0; } ...

  10. mysql.msi安装流程

    Mysql For Windows安装图解 演示安装版本:mysql-5.5.20-win32.msi(目前是mysql for windows的最新版)安装环境:Windows Server 200 ...