[抄题]:

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函数放进去
  1. Arrays.asList("..",".","")

[二刷]:

  1. deque非空才能POP

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

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

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

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

从左往右走:

  1. "/b/c/" - directory 'b ' - > directory 'c '
  2. "." - current directory
  3. "./" - current directory
  4. "../" - one directory up
  5. e.g
  6. "/" : root directory
  7. "b/c/../" : it will go from c to b
  8. "c/b/./" : it is still in directory b

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

[关键模板化代码]:

[其他解法]:

[Follow Up]:

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

[代码风格] :

  1. class Solution {
  2. public String simplifyPath(String path) {
  3. //cc
  4.  
  5. //ini: deque, set,put into deque
  6. Deque<String> deque = new LinkedList<>();
  7. Set<String> set = new HashSet<>(Arrays.asList("",".",".."));
  8. for (String w : path.split("/")) {
  9. if (w.equals("..") && !deque.isEmpty()) deque.pop();
  10. else if (!set.contains(w)) deque.push(w);
  11. }
  12.  
  13. //get from deque, res
  14. String res = "";
  15. for (String words : deque)
  16. res = "/" + words + res;
  17. return res.isEmpty() ? "/" : res;
  18. }
  19. }

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. Django ORM哪些操作

    一般操作 看专业的官网文档,做专业的程序员! 必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 ...

  2. [Oracle] CPU/PSU补丁安装详细教程

    Oracle CPU的全称是Critical Patch Update, Oracle对于其产品每个季度发行一次安全补丁包,通常是为了修复产品中的安全隐患,以下是对CPU/PSU补丁安装的具体操作步骤 ...

  3. Linux 制作补丁 打补丁 撤销补丁

    1.制作补丁 diff - 逐行比较文件 格式 diff   参数   旧文件/旧文件夹   新文件/新文件夹 -N   将不存在的文件看作是空的 -a   将所有文件都视为文本文件 -u   以合并 ...

  4. 基于INTEL FPGA硬浮点DSP实现卷积运算

    概述 卷积是一种线性运算,其本质是滑动平均思想,广泛应用于图像滤波.而随着人工智能及深度学习的发展,卷积也在神经网络中发挥重要的作用,如卷积神经网络.本参考设计主要介绍如何基于INTEL 硬浮点的DS ...

  5. Linux 设备总线驱动模型

    尽管LDD3中说对多数程序员掌握设备驱动模型不是必要的,但对于嵌入式Linux的底层程序员而言,对设备驱动模型的学习非常重要.     Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一 ...

  6. 使用wifi网卡笔记4---工具dhcp及全自动使用wifi

    dhcp : 使得WIFI网卡动态获取IP ·1.源码获取 输入dh看有哪些应用程序,输入dhclient -v查看一下相关信息,源码获取的网址 2.开发环境配置 解压 tar xzf dhcp-4. ...

  7. SQL、PL/SQL、DDL、DML、TCL介绍

    SQL:结构化查询语言(Structured Query Language) PL/SQL:过程化SQL语言(Procedural Language/SQL) DDL(Data Definition ...

  8. php Reflection

    <?php function title($title, $name) { return sprintf("%s. %s\r\n", $title, $name); } $f ...

  9. freebsd静态路由

    FreeBSD下增进静态路由的行动 1.手工添加 # route add -net 192.168.2.0/24 192.168.1.2 2. 通过rc.conf永世 设置 # Add static ...

  10. MYSQL用户权限管理GRANT使用

    http://yanue.net/post-97.html GRANT语句的语法: mysql> grant 权限1,权限2,-权限n on 数据库名称.表名称 to 用户名@用户地址 iden ...