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. java poi 写入大量数据到excel中

    最近在利用poi往excel中写入大量数据时,发现excel2003最多只支持65535条,大量数据时容易造成oom,上网查了一下api,发现目前对于2003,每个sheet最多支持65535条,若数 ...

  2. 一张图让你学会Python

    有编程基础的人一看就可以了解 Python 的用法了.真正的 30 分钟上手.国外一高手画的,现把它翻译成中文,入门超简单python入门神图 *单击放大

  3. html5、canval 对 图片的压缩

    let src = this.cropper.getCroppedCanvas().toDataURL('image/jpeg');let can = document.createElement(' ...

  4. 截图保存 matlab

    file=dir('D:\Program Files\MATLAB\R2014a\bin\MyPro\OCR\图像中时间识别\')for i=3:length(file) path= strcat(' ...

  5. windows下perl的安装和脚本的运行

    参考 1.windows下perl的安装和脚本的运行: 2.fddb测试fddb的评估方法: 3.gunplot5.2.4-download: 完

  6. 20155338 2016-2017-2 《Java程序设计》第8周学习总结

    20155338 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 一.日志 API简介 java++.util.logging包提供了日志功能相关类与接口,不 ...

  7. 各大OJ题目分类

    http://www.pythontip.com/acm/problemCategory

  8. hibernate连接oracle12c数据库报:java.sql.SQLException: ORA-01017: 用户名/口令无效; 登录被拒绝。(用户名/口令在oracle客户端以及cmd命令都能登入)

    报错信息: 2017-09-22 15:40:07,354 WARN [org.hibernate.cfg.SettingsFactory] - Could not obtain connection ...

  9. test20180828

    所有试题限制都为512MB,1Sec 总分230. 试题1 新的开始 [题目描述] 发展采矿业当然首先得有矿井, 小FF花了上次探险获得的千分之一的财富请人在岛上挖了n口矿井, 但他似乎忘记考虑的矿井 ...

  10. USB速率识别

    低速设备D-上有一个1.5k欧的上拉电阻.高速和全速设别在D+上有一1.5k欧上拉电阻.连接后通过检测电压变化来了解设备是否为低速设别. 低速下:D+为“0”,D-为“1”是为“J”状态,“K”状态相 ...