leetcode面试准备:Simplify Path

1 题目

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

For example,

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

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

Corner Cases:

Did you consider the case where path = "/../"?

In this case, you should return "/".

Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".

In this case, you should ignore redundant slashes and return "/home/foo".

接口:public String simplifyPath(String path)

2 思路

简化linux路径,主要对".." 进行回退操作。直观的想到用栈,在java里,栈用Deque的实现类LinkedList来做。

  1. path进行split
  2. stack中添加路径,进栈,出栈
  3. 最后将stack中元素输出

3 代码

    public String simplifyPath(String path) {
String[] words = path.split("/");
List<String> stack = new LinkedList<String>();
for (String s : words) {
if (s.equalsIgnoreCase("..")) {
if (!stack.isEmpty())
stack.remove(stack.size() - 1);
} else if (!(s.length() == 0 || s.equalsIgnoreCase("."))) {
stack.add(s);
}
}
String res = "";
for (String s : stack) {
res += "/" + s;
}
return res.length() == 0 ? "/" : res;
}

4 总结

栈的思想解决。

leetcode面试准备:Simplify Path的更多相关文章

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

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

  2. 【LeetCode】71. Simplify Path

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

  3. 【一天一道LeetCode】#71. Simplify Path

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. LeetCode OJ:Simplify Path(简化路径)

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

  5. [LintCode] Simplify Path 简化路径

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

  6. leetcode面试准备:Triangle

    leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...

  7. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  8. 56. Edit Distance && Simplify Path

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

  9. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

随机推荐

  1. 第十二篇、高度自适应的textView

    高度根据输入内容变化输入框,我们在很多的应用上都可以见到,如微信.QQ聊天,QQ空间评论等等,该输入框可以用xib,纯代码编写,但是个人觉得纯代码编写用起来更加方便一些. 1.使用自定义的UIView ...

  2. Walkthrough: Creating and Using a Dynamic Link Library (C++)

    Original Link: http://msdn.microsoft.com/zh-cn/library/ms235636.aspx Following content is only used ...

  3. Fire Net

    Fire Net Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  4. 关于使用工具类org.apache.commons.collections.ListUtils合并List的问题

    今天在做项目时,需要将几个List进行合并,于是就用到了apache提供关于List操作的工具类ListUtils,但是在使用的过程中发现一些问题. public static void main(S ...

  5. C语言使用中的细节问题总结

    1.在使用bool关键字时,出现"error:'bool' undeclared(first use in this function)"的错误,原因为C语言本身是没有bool关键 ...

  6. React:用于搭建UI的JavaScript库

    React https://facebook.github.io/react/index.html 2016-08-03 先吐槽一下.看过很多博客.教程.文章,一直想不通为什么大牛们介绍一种新技术一上 ...

  7. JS判断上传图片格式是否正确

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. Linux主机在LNMP环境中同时运行多个PHP版本

    这次遇到的问题是,客户网站已经使用PHP5.4运行多个网站程序,但是新安装的程序需要使用PHP5.3. 从我之前的经验来看,给网站更换PHP版本,可能会带来意想不到的后果.比如,之前某客户Discuz ...

  9. 一些SVN 地址

    lockbox3 地址: https://svn.code.sf.net/p/tplockbox/code/trunc indy10 地址:https://svn.atozed.com:444/svn ...

  10. 如何在JS中获取Request方法

    方法 function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = ne ...