zoukankan      html  css  js  c++  java
  • 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

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    寒假作业4
    UVA5870 乱搞 Smooth Visualization
    UVA5874 Social Holidaying 二分匹配
    UVA5876 Writings on the Wall 扩展KMP
    hdu1231 最大连续子序列
    hdu3535 混合背包
    hdu3613 扩展KMP
    hdu4333 扩展KMP
    扩展KMP
    hdu4287 字典树
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11781497.html
Copyright © 2011-2022 走看看