[抄题]:

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

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

空格属于不需要输出的特例,所以先存到hashset中

deque中啥也没有,就输出/

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".

[思维问题]:

[一句话思路]:

用push pop从一端把该存的存入,用for循环从另一端输出

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. hashset<>(),括号中表示要构造的内容,把array函数放进去
Arrays.asList("..",".","")

[二刷]:

  1. deque非空才能POP

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

用push pop从一端把该存的存入,用for循环从另一端输出

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

从左往右走:

"/b/c/" - directory 'b ' - > directory 'c '
"." - current directory
"./" - current directory
"../" - one directory up
e.g
"/" : root directory
"b/c/../" : it will go from c to b
"c/b/./" : it is still in directory b

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public String simplifyPath(String path) {
//cc //ini: deque, set,put into deque
Deque<String> deque = new LinkedList<>();
Set<String> set = new HashSet<>(Arrays.asList("",".",".."));
for (String w : path.split("/")) {
if (w.equals("..") && !deque.isEmpty()) deque.pop();
else if (!set.contains(w)) deque.push(w);
} //get from deque, res
String res = "";
for (String words : deque)
res = "/" + words + res;
return res.isEmpty() ? "/" : res;
}
}

71. Simplify Path压缩文件的绝对路径的更多相关文章

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

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

  2. 71. Simplify Path(M)

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

  3. 【LeetCode】71. Simplify Path

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

  4. [LeetCode] Simplify Path,文件路径简化,用栈来做

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

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

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

  6. 71. Simplify Path

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

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

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

  8. Leetcode#71 Simplify Path

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

  9. 71. Simplify Path (Stack)

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

随机推荐

  1. 笔记:NPM 无限需要依赖问题解决

    笔记:NPM 无限需要依赖问题解决 起因 因为想学一下 VUE,开始跟着教程一步一步输出命令,开始也没有什么问题,一切都很顺利. 突然不知道是哪一步出了问题,一直让我安装依赖,没完没了,开始并不觉得有 ...

  2. bzoj1025(SCOI2009)游戏——唯一分解的思路与应用

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1025 可以认为对应的值之间连边,就连成了一个有一个或几个环的图.列数就是每个环里点数的lcm ...

  3. 在AWS EMR上运行Map Reduce的Java示例程序 及 操作小计

    下面的代码中AffairClient类中包含了三个内之类,分别对应于Hadoop Mapreduce程序运行所需的Mapper类,Reducer类,和主类.AffairClient类中其余方法用于配置 ...

  4. Mac环境下PHPstorm配置xdebug开发调试web程序

    一.安装PHP的xdebug扩展 安装xdebug(技巧,为了找到适配的版本,让xdebug网站根据phpinfo()函数输出分析找到对应的方法及安装步骤:如果安装了多个PHP版本的话,尽量用phpi ...

  5. 保证service不被杀死的方法

    Service设置成START_STICKY kill 后会被重启(等待5秒左右),重传Intent,保持与重启前一样 提升service优先级 在AndroidManifest.xml文件中对于in ...

  6. java代码-----------继承练习

    总结:父类和子类拥有相同的 方法时,父类的方法被覆盖,子类 package com.sads; class fong { void pprint() { this.print(); this.prin ...

  7. MariaDB主从异步复制详解

    一 异步复制(Asynchronous replication) 1.MariaDB本身支持单向的.异步的复制.异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时,最重要的是这意味着当应用系 ...

  8. FBString

    folly/FBString.h fbstring is a drop-in replacement for std::string. The main benefit of fbstring is ...

  9. oracle autotrace使用

    通过以下方法可以把Autotrace的权限授予Everyone, 如果你需要限制Autotrace权限,可以把对public的授权改为对特定user的授权. D:\oracle\ora92>sq ...

  10. Java string String

    java.lang.String string这个不是关键字 关String的讲解,参看:http://www.cnblogs.com/octobershiner/archive/2012/04/02 ...