zoukankan      html  css  js  c++  java
  • 71. Simplify Path

    这个题有俩EDGE CASE不容易想到。
    一个是没有路径是返还“/"不是""
    另一个是有可能出现a//b这种情况,split会分出一个空字符串

    然后就是EASY难度的了。。

    public class Solution {
        public String simplifyPath(String path) 
        {
            if(path.length()  == 0) return "/";
            
            Stack<String> stk = new Stack<>();
            
            String[] str = path.split("\/");
            
            for(int i = 0; i < str.length;i++)
            {
                if(str[i].equals(".."))
                {
                    if(!stk.isEmpty()) stk.pop();
                }
                else if(str[i].equals(".") || str[i].equals(""))
                {
                    
                }
                else
                {
                    stk.push(str[i]);
                }
            }
            String res = "";
            
            if(stk.size() == 0) return "/";
            else
            {
                while(!stk.isEmpty())
                {
                    res = "/" + stk.pop() + res;
                }
            }
            
            return res;
            
        }
    }
    

    split里面怎么写应该把空格,斜杠这种特殊的记住。。。

  • 相关阅读:
    poj3255,poj2449
    poj2186
    poj3249
    poj3378
    poj3274
    poj1948
    hdu 2181暴搜
    hdu 3342
    hdu 1285
    hdu 1598
  • 原文地址:https://www.cnblogs.com/reboot329/p/5876122.html
Copyright © 2011-2022 走看看