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. Goolge全球各国域名大全

    搜索过技术文章的朋友都知道,Google的搜索功力绝对是世界第一,仅仅搜索中文还体现不出Google的功力,如果大家是做外贸或者搜索其它语言的文章,就会知道Google的内涵有多少了.全世界的国家Go ...

  2. vue 和 react 选择

    经典 react 还是有其优点的, 生态更丰富,  更容易 测试 和 调试, 更适合超大型应用,  更适合app,  weex不一定能到达 reactNative的高度 而vue , 更为直观, 上手 ...

  3. SWIFT中函数返回值为Tuple

    在playgroundm内键入以下代码,求一个成绩数组内最大分值和最小分值 func maxminScore(scores:Array<Int>) -> (maxScore:Int, ...

  4. UI基础:UITableView表视图

    表视图 UITableView,iOS中最重要的视图,随处可见. 表视图通常用来管理一组具有相同数据结构的数据. UITableView继承于UIScrollView,所以可以滚动 表视图的每条数据都 ...

  5. Javascript鼠标事件大全

    事件 浏览器支持 描述 onClick IE3|N2|O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDblClick IE4|N4|O 鼠标双击事件 onMouseDown IE4|N ...

  6. OK335xS GPMC nand device register hacking

    /********************************************************************************* * OK335xS GPMC na ...

  7. ULINK2配置

    先要安装ULINK2的驱动 如果还没有检测到驱动的话,下个驱动人生,应该就行了.反正我就是这样弄成的^-^. 然后就是配置了 这样就可以下载了.

  8. 在 Ubuntu 18.0-10上安装 MySQL8

    直接使用apt install mysql-server安装,那么恭喜你踩坑. sudo apt install mysql-server默认会安装MySQL 5.7,将会出现一些莫名的问题,例如:安 ...

  9. Codeforces 617B:Chocolate(思维)

    题目链接http://codeforces.com/problemset/problem/617/B 题意 有一个数组,数组中的元素均为0或1 .要求将这个数组分成一些区间,每个区间中的1的个数均为1 ...

  10. django 远程数据库mysql migrate失败报error 1045之 解决方案

    Access denied for user 'root'@'localhost' (using password: YES)       ERROR 1045: Access denied for ...