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

For example,

path = "/home/", => "/home"

path = "/a/./b/../../c/", => "/c"

算法:根据/把path分割,再逐个判断

public class Solution {
public String simplifyPath(String path) {
String[] strs = path.split("/");
Stack<String> s = new Stack<String>();
for(int i=0;i<strs.length;i++){
if(strs[i].equals(".")){
}else if(strs[i].equals("..")){
if(!s.isEmpty())
s.pop();
}else if(strs[i].length()>0){
s.push(strs[i]);
}
}
StringBuilder sb = new StringBuilder();
Iterator<String> it = s.iterator();
while(it.hasNext()){
String t = it.next();
sb.append("/").append(t);
}
String r=sb.toString();
if(r.length()==0)
r+="/";
return r;
}
}

Simplify Path的更多相关文章

  1. [LintCode] Simplify Path 简化路径

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

  2. 56. Edit Distance && Simplify Path

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

  3. leetcode面试准备:Simplify Path

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

  4. 71. Simplify Path(M)

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

  5. 【LeetCode】71. Simplify Path

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

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

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

  7. [LeetCode] Simplify Path 简化路径

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

  8. 【leetcode】Simplify Path

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

  9. Leetcode Simplify Path

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

随机推荐

  1. 2015.5.2-2015.5.8 Tip jQuery ,前端组件库,inline-block元素间距等

    有忙于它事,故延迟了,但在坚持! 1.Tip jQuery   2.给span加display: inline-block; 怎样能对齐? 解决方法:vertical-align: bottom:   ...

  2. mysql [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist (转载)

    mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 2013-11-2 ...

  3. JSP内置对象之application对象

    虽然常把Web应用称为B/S架构的应用,但其实Web应用一样是C/S结构的应用,只是这种应用的服务器是Web服务器,而客户端是浏览器. 现在抛开Web应用直接看Web服务器和浏览器. Web服务器负责 ...

  4. PHP错误级别 error_reporting() 函数详解

    在PHP开发的时候常常会用到error_reporting(report_level)来调试自己的程序,下面列出了report_level可能值: 值 常量 描述 1 E_ERROR 这是一个严重错误 ...

  5. ID属性值为小数

    获取带有.的id值 <h1 id="123.45">dom对象</h1> <script> $('#123\\.45').attr('id') ...

  6. mapReduce编程之auto complete

    1 n-gram模型与auto complete n-gram模型是假设文本中一个词出现的概率只与它前面的N-1个词相关.auto complete的原理就是,根据用户输入的词,将后续出现概率较大的词 ...

  7. android studio--百度定位集成001

    安卓现在的大趋势已经是普遍使用androidstudio(安装包[https://yunpan.cn/ckc54idj3JVJb  访问密码 664f])了.这个是集成的一个好的环境. 今天来搞个百度 ...

  8. 开发基于Edge渲染内核的浏览器应用

    在使用Edge之前,我们先来看看UWP(Universal Windows Platform)平台.微软研发了多种设备平板.手机.Xbox.个人电脑等,在此之前,如果需要给每台设备开发程序,都需要对应 ...

  9. Sublime 3 如何设置xftp 保存自动上传

    采用sublime SFTP插件,直接保存CentOS服务器的NodeJS文件,这样调试起来很方便,当然得配上 一直采用File  ->   SFTP/FTP  ->   Setup Se ...

  10. 拨打电话tel: 跳转到邮件mailto:(html)

    拨打电话 <a href="tel://0571866000">0571-866000</a> 跳转到邮件 <a href="mailto: ...