题目简述:

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

For example,

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

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

click to show corner cases.

解题思路:

很明显是压栈弹栈的过程,难点在特殊情况要考虑周全,corner cases基本提示全了

class Solution:
# @param path, a string
# @return a string
def simplifyPath(self, path):
s = path.strip().split('/')
sta = []
for i in s:
if i == '..':
if len(sta) > 0:
sta.pop()
else:
pass
elif i == '.':
pass
elif i == '':
pass
else:
sta.append(i)
return '/'+'/'.join(sta) s = Solution()
print s.simplifyPath("//home/")
print s.simplifyPath("/a/./b/../../c/")
print s.simplifyPath("/home/../../..")

【leetcode】Simplify Path的更多相关文章

  1. 【LeetCode】113. Path Sum II 解题报告(Python)

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

  2. 【Leetcode】【Medium】Simplify Path

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

  3. 【字符串】Simplify Path(栈)

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

  4. 【LeetCode】437. Path Sum III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...

  5. 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...

  6. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  7. 【leetcode】Minimum Path Sum(easy)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. 【LeetCode】112. Path Sum

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

随机推荐

  1. [分类算法] :朴素贝叶斯 NaiveBayes

    1. 原理和理论基础(参考) 2. Spark代码实例: 1)windows 单机 import org.apache.spark.mllib.classification.NaiveBayes im ...

  2. STM32库函数编程、Keli/MDK、stm32f103zet6

    catalogue . Cortex-M3地址空间 . 基于标准外设库的软件开发 . 基于固件库实现串口输出(发送)程序 . 红外接收实验 . 深入分析流水灯例程 . GPIO再举例之按键实验 . 串 ...

  3. <<< Google hack

    使用Google等搜索引擎对某些特定的网络主机漏洞(通常是服务器上的脚本漏洞)进行搜索,以达到快速找到漏洞主机或特定主机的漏洞的目的. 在SEO优化中,通常使用这种技术达到入侵一些网站挂外链之用.黑帽 ...

  4. oracle--知识点汇总2---laobai

    --复制表 create table emp as(select * from scott.emp); select * from emp; --Demo1创建存储过程,实现将emp表comm为空时, ...

  5. centos 7.0 nginx 1.7.9成功安装过程

    centos 7.0根目录 的目录构成 [root@localhost /]# lsbin dev home lib64 mnt proc run srv tmp varboot etc lib me ...

  6. [UML]UML系列——类图Class

    相关文章       [UML]UML系列——用例图Use Case [UML]UML系列——用例图中的各种关系(include.extend) 一.类图的概念及组成 1.类图的概念 类图是描述类.接 ...

  7. Ajax 应用六个需要注意的事项

    接触Ajax,那时候的Ajax支持还不是很好,都要涉及底层,没有现成的框架给你调用.现在把常见的问题列举如下.1.编码问题注意AJAX要取的文件是UTF-8编码的.GB2312编码传回BROWSE后中 ...

  8. Linux网络下载命令 wget 简介

    wget 是一个命令行的下载工具.对于我们这些 Linux 用户来说,几乎每天都在使用它.下面为大家介绍几个有用的 wget 小技巧,可以让你更加高效而灵活的使用 wget. $ wget -r -n ...

  9. Python转码问题的解决方法:UnicodeDecodeError:‘gbk' codec can't decode bytes in position

    在开发过程中遇到了错误:UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 678-679...这是因为遇到了非法字符, 解决 ...

  10. semantic modal 首次弹出位置不正确()

    暂不知是什么原因,先记录下,可以用下面这句css解决 .ui.modal{ %; }