题目简述:

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. extracting fasta records from a multi-fasta file based on a list using awk

    for i in $(cat gene_list) do awk -v RS=">" '($1==a){print ">"$0}' a=$i inp ...

  2. mui日期插件$, each遍历,EventListener事件监听,json格式

    (function($) { $.init(); var result = $('#result')[0]; var btns = $('.btn'); btns.each(function(i, b ...

  3. Interface小例子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  4. MapReduce实现手机上网流量分析(业务逻辑)

    一.问题背景 现在的移动刚一通话就可以在网站上看自己的通话记录,以前是本月只能看上一个月.不过流量仍然是只能看上一月的. 目的就是找到用户在一段时间内的上网流量. 本文并没有对时间分组.下一节进行分区 ...

  5. Java 对象 及 对象的应用

    http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=30149799&id=4942380原文地址

  6. [Unity3D]引擎学习之注意事项(持续更新中)

    调试相关 如果是想在触发粒子系统效果的时候播放声音(比如爆炸的特殊发生时也播放声音),则需要将爆炸效果的粒子系统保持为Prefab后,添加Audio Source组件,在组件中添加声音文件并且确保pl ...

  7. 概率论与数理统计图解.tex

    \documentclass[UTF8,a1paper,landscape]{ctexart} \usepackage{tikz} \usepackage{amsmath} \usepackage{a ...

  8. WordPress数据库优化技巧

    各位站长都知道wordpress用久了就会越来越慢.今天就给大家介绍如何给自己的wordpress提速,分两种方法:1.插件属性wordpress的都知道其插件是相当的多,只要你能想得到的基本都有,在 ...

  9. php函数fgets读取文件

    如果一个文件比较大,可以考虑用fgets函数 下面是个例子: #文件作用:fgets读取文件 $start_time = microtime(true); $file_name = "a.t ...

  10. LYDSY模拟赛day1 Walk

    /* 依旧考虑新增 2^20 个点. i 只需要向 i 去掉某一位的 1 的点连边. 这样一来图的边数就被压缩到了 20 · 2^20 + 2n + m,然后 BFS 求出 1 到每个点的最短路即可. ...