Leetcode 之Simplify Path @ python

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 = 0
        res = ‘‘
        while i< len(path):
            end = i+1
            while end<len(path) and path[end] !="/":
                end += 1
            sub = path[i+1:end]
            if len(sub)>0:
                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();
}
时间: 2024-12-20 17:23:44

Leetcode 之Simplify Path @ python的相关文章

[leetcode]Simplify Path @ Python

原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corn

【LeetCode】Simplify Path

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

leetcode 71 Simplify Path

题目连接 https://leetcode.com/problems/simplify-path/ Simplify Path Description Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

leetCode 71.Simplify Path(化简路径) 解题思路和方法

Simplify Path 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: Did you consider

[C++]LeetCode: 117 Simplify Path (简化Unix路径 list双向链表)

题目: 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: Did you consider the case w

[LeetCode] 71. Simplify Path 简化路径

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" Corner Cases: Did you consider the case where path = "/../"?In th

[LeetCode][Java] Simplify Path

题目: 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: Did you consider the case w

LeetCode[Stack]: Simplify Path

Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" 这个题目比较简单,用很常规的方法解决即可,测试的时候遇到什么问题再解决就行了.我的C++代码如下: string simplifyPath(str

leetcode 71 Simplify Path ------ java

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: Did you consider the case where p