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. tyvj1087 sumsets

    背景 广东汕头聿怀初中 Train#2 Problem1 描述     正整数N可以被表示成若干2的幂次之和.例如,N = 7时,共有下列6种不同的方案:1) 1+1+1+1+1+1+12) 1+1+ ...

  2. ASP.NET 导出数据表格

    功能:可以实现导出整个数据表格或整个页面 public bool ExportGv(string fileType, string fileName)        {            bool ...

  3. Rails的三种环境----开发环境,生产环境和测试环境

    Rails 的三个环境 Rails 的应用程序预设提供了三种不同的执行模式: development environment 开发模式,用在你的开发的时候 test environment 测试模式, ...

  4. 使用phpMyAdmin修改MySQL数据库root用户密码

    点击顶部的“SQL”标签进入sql命令输入界面.输入以下命令: update mysql.user set password=PASSWORD('snsgou$123456') where user= ...

  5. Scrum Meeting ——总结

    冲刺总结 0*.燃尽图 迟来的燃尽图,别看它是最后一天掉了一堆,感觉很假,像是人为的把issues都关闭掉.其实不然,很多功能是大家平时做好,但是没整合在一起,所以没燃掉,在最后几天的整合中,通过测试 ...

  6. chrome地址栏搜索直接跳转百度首页?

    https://www.baidu.com/s?ie={inputEncoding}&wd=%s

  7. C/C++ 结构体内存对齐

    内存对齐是指的是编译器在编译的时候总是会将结构体的元素的地址放在一些合适的位置使得CPU访问这些数据的效率变得更高.首先来看下面这个例子: struct A{ char a; char b; int ...

  8. word20161213

    journal queue / 日志队列 journal quota / 日志配额 junction point / 交叉点 KDC, Key Distribution Center / 密钥分发中心 ...

  9. C#夯实基础系列之const与readonly

    一.const与readonly的争议       你一定写过const,也一定用过readonly,但说起两者的区别,并说出何时用const,何时用readonly,你是否能清晰有条理地说出个一二三 ...

  10. html中表格元素的相关总结

    表格元素相关总结: 1.在CSS中,内部表元素(如td.tr.col等)生成矩形框,这些矩形框包含内容.内边距和边框,但没有外边距,因此如果定义外边距,浏览器将忽略该定义:对于table元素,外边距有 ...