Leetcode 之Simplify Path @ python

來源:互聯網
上載者:User

標籤:

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();}

 

Leetcode 之Simplify Path @ python

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.