题目简述:

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. SHOI2016游记&滚粗记&酱油记

    Day0 学校刚期中考完,全科血崩,感觉这次真要考不到一本线了tat 晚上写了个可持久化trie的题,也懒得敲板子(上个礼拜都敲过了),就碎叫了 Day1 上午起床吃饭水群看球,吃完中饭就去考场了. ...

  2. 【BZOJ-4082】Surveillance 树链剖分 LCA + 贪心

    4082: [Wf2014]Surveillance Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 260  Solved: 100[Submit][ ...

  3. div

    <div id="container"> <div id="left">左边</div> <div id=" ...

  4. sql 从一个库中取某个表的数据导入到另一个库中相同结构的表中

    sql 2008 从一个库中把 某个表中的数据导入到另一个库中的具有相同结构的表中 use 库1 go insert into  库1.dbo.表1  select * from  库2.dbo.表1 ...

  5. Linux ip

    工具/原料 linux系统 putty 方法/步骤   Linux下查看IP一般都是用命令在终端查看了,使用命令行来进行查看.   想要在图形界面查看的朋友也有办法,不过就比较复杂,不如一条命令来得痛 ...

  6. 创建16x16二级hash目录

    Hash目录是一种优化文件存储性能的方法.无论是Windows还是Linux,无论是NTFS还是ext4,每个目录下所能容纳的项目数是有限的.并不是不能保存,而是当项目数量过大的时候,会降低文件索引速 ...

  7. 解决ArcGIS安装之后出现的Windows installer configures问题

    ----Please wait while Windows installer configures ArcGIS Desktop Error Message错误信息 When launching A ...

  8. sublime2使用jshint

    合理配置Jshint可以帮助写出高质量的代码,通过sublime2插件 JSHint Gutter 可以迅速提供开发效率和减少bug的个数. 1.安装JSHint Gutter插件 sublime2按 ...

  9. Redis主-从部署实践

    0. 前言 这篇文章简要介绍Redis的主从部署,实现了一主二从,使用两个哨兵监控,以实现简单的HA,其中从库作为备机. 1. 部署 这里有三台服务器,其中239主机上的Redis作为主库,其余两个作 ...

  10. SQL Server 2012复制教程以及复制的几种模式

    简介 SQL Server中的复制(Replication)是SQL Server高可用性的核心功能之一,在我看来,复制指的并不仅仅是一项技术,而是一些列技术的集合,包括从存储转发数据到同步数据到维护 ...