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

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

使用一个栈来解决问题。遇到'..'弹栈,遇到'.'不操作,其他情况下压栈。

代码一:

class Solution:
# @param path, a string
# @return a string
def simplifyPath(self, path):
stack = []
i =
res = ''
while i< len(path):
end = i+
while end<len(path) and path[end] !="/":
end +=
sub = path[i+:end]
if len(sub)>:
if sub == "..":
if stack !=[]:
stack.pop()
elif sub != ".":
stack.append(sub)
i = end if stack == []:
return "/"
for i in stack:
res += "/"+i
return res

code 2:

class Solution:
def simplifyPath(self,path):
path = path.split('/')
res = '/'
for i in path:
if i == '..':
if res != '/':
res = '/'.join(res.split('/')[:-1])
if res =='': res = '/'
elif i != '.' and i != '':
res += '/' +i if res != '/' else i
return res

转自(参考):

1. http://www.cnblogs.com/zuoyuan/p/3777289.html

2. http://blog.csdn.net/linhuanmars/article/details/23972563

@ JAVA 版本

public String simplifyPath(String path) {
if(path == null || path.length()==0)
{
return "";
}
LinkedList<String> stack = new LinkedList<String>();
StringBuilder res = new StringBuilder();
int i=0; while(i<path.length())
{
int index = i;
StringBuilder temp = new StringBuilder();
while(i<path.length() && path.charAt(i)!='/')
{
temp.append(path.charAt(i));
i++;
}
if(index!=i)
{
String str = temp.toString();
if(str.equals(".."))
{
if(!stack.isEmpty())
stack.pop();
}
else if(!str.equals("."))
{
stack.push(str);
}
}
i++;
}
if(!stack.isEmpty())
{
String[] strs = stack.toArray(new String[stack.size()]);
for(int j=strs.length-1;j>=0;j--)
{
res.append("/"+strs[j]);
}
}
if(res.length()==0)
return "/";
return res.toString();
}

Leetcode 之Simplify Path @ python的更多相关文章

  1. [leetcode]Simplify Path @ Python

    原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...

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

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

  3. 【leetcode】Simplify Path

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

  4. Java for LeetCode 071 Simplify Path

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

  5. Leetcode#71 Simplify Path

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

  6. leetcode[70] Simplify Path

    题目的意思是简化一个unix系统的路径.例如: path = "/home/", => "/home"path = "/a/./b/../../ ...

  7. Leetcode 之Simplify Path(36)

    主要看//之间的内容:如果是仍是/,或者是.,则忽略:如果是..,则弹出:否则压入堆栈.最后根据堆栈的内容进行输出. string simplifyPath(string const& pat ...

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

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

  9. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

随机推荐

  1. 团队项目之UML图设计

    团队 学号 姓名 本次作业博客链接 031602428 苏路明(组长) https://www.cnblogs.com/Sulumer/p/9822854.html 031602401 陈瀚霖 htt ...

  2. NodeJS 难点(网络,文件)的 核心 stream 一:Buffer

    stream应用一图片转存服务 stream github教程 文件操作和网络都依赖了一个很重要的对象—— Stream, 而这个 <node深入浅出> 没有分析的, 所以读完这本书, 在 ...

  3. 配置ssh互信的一个小问题记录

    在我们安装集群软件时,经常需要配置机器之间的互信,每个人也有一套自己熟悉的方法. 但是在今天配置互信过程中,发现操作过程还是一样,但是就是不能互信,通过查找资料解决了,做一下记录. 我经常使用的方式: ...

  4. FMX StringGrid向上滑动自动加载记录(一)

    有时候,做的app还是需要用StringGrid来显示数据,但如果用StringGrid的Livebinding绑定到一个数据集TDataset,当记录超过1000条时,效率非常低,甚至达不到实用状态 ...

  5. C#foreach的用法

    static void Main(string[] args)        {            int[] a = new int[5] { 1, 2, 3, 4, 5 };          ...

  6. net start mysql启动mysql,提示发生系统错误 5 拒绝访问 解决方法

    解决问题方法如下: 在dos下运行net  start mysql 不能启动mysql!提示发生系统错误 5:拒绝访问!切换到管理员模式就可以启动了.所以我们要以管理员身份来运行cmd程序来启动mys ...

  7. Python 字符串的相关操作

    s = "abcdefg" s1 = s.capitalize() # 把首字母变成大写, 返回给你一个新的字符串 print(s) print(s1) s = "ale ...

  8. python 学习日志

    1.pip is already installed if you're using Python 2 >=2.7.9 or Python 3 >=3.4 binaries downloa ...

  9. HDU1024 最大M子段和问题 (单调队列优化)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. HDU1702:ACboy needs your help again!

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...