leetcode——71. 简化路径

class Solution(object):
    def simplifyPath(self, path):
        """
        :type path: str
        :rtype: str
        """
        stack=[]
        path=path.split('/')
        for item in path:
            if item=='..':
                if stack:stack.pop()
            elif item and item!='.':
                stack.append(item)
        return '/'+'/'.join(stack)
执行用时 :28 ms, 在所有 python 提交中击败了64.37%的用户
内存消耗 :11.6 MB, 在所有 python 提交中击败了30.41%的用户
 
——2019.11.2
 

稀里糊涂地做了一下,并不清楚是怎么回事
public String simplifyPath(String path) {
        Stack<String> stack = new Stack<>();
        //对path以/进行分割
        String[] str = path.split("/");
        for(String s : str){
            if(s.equals("..")){
                if( !stack.isEmpty()) {
                    stack.pop();
                }
            }else if(!s.equals("") && !s.equals(".")){
                stack.push(s);
            }
        }
        if(stack.isEmpty()){
            return "/";
        }
        return '/'+String.join("/",stack);
    }

 

 

——2020.7.8

posted @ 2019-11-02 13:38  欣姐姐  阅读(148)  评论(0编辑  收藏  举报