• 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 path = "/../"?
      In this case, you should return "/".
    • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
      In this case, you should ignore redundant slashes and return "/home/foo".
    这道题就是化简地址,有两个规则:  /./  代表不变
                      /../   代表返回上级目录
    还有就是Corner Cases中的特殊情况都有说明
     
     
    这道题刚开始写了好半天,其实主要原因是,题目中有一个没有说明,那就是带有转义字符的情况:如果目录名字中含有‘/’,如果考虑这种情况会复杂许多。但是这道题其实没有考虑这种情况,也就是说将这个符号当成一般的字符处理。所以也就没什么难点,就是考虑清楚情况。
    可以使用自动机求解。
    public class Solution {
        public String simplifyPath(String path) {
            int len = path.length();
            if (len == 0)
                return path;
            Stack<Character> stack = new Stack<Character>();
            int i = 0;
            while (i < len) {
                if (path.charAt(i) == '/') {
                    while( i<len && path.charAt(i) == '/')
                            i++;
                    i--;
                    if( i+1 == len)
                        i++;
                    else if( path.charAt(i+1) == '.'){
                        if( i+2 == len || path.charAt(i+2) == '/')
                            i+=2;
                        else if( path.charAt(i+2) == '.'){
                            if( i+3 == len || path.charAt(i+3) == '/'){
                                int j = 0;
                                
                                while( j<stack.size() )
                                    if(  stack.pop() == '/')
                                        break;
                                    j++;
                                
                                i+=3;
                            }else{
                                stack.push('/');
                                stack.push('.');
                                stack.push('.');
                                i+=3;
                                while (i < len) {
                                    if (path.charAt(i) == '/')
                                        break;
                                    else 
                                        stack.push(path.charAt(i));
                                    i++;
                                }
                            }
                        }else{
                            stack.push('/');
                            stack.push('.');
                            i+=2;
                            while (i < len) {
                                if (path.charAt(i) == '/')
                                    break;
                                else 
                                    stack.push(path.charAt(i));
                                i++;
                            }
                        
                        }
                            
                    }else{
                        i++;
                        stack.push('/');
                        
                        while (i < len) {   
                            if (path.charAt(i) == '/')
                                break;
                            else 
                                stack.push(path.charAt(i));
                            i++;
                        }
                    }
                } else {
                    if( path.charAt(i) == '.'){
                        if( i+1 == len || path.charAt(i+1) == '/')
                            i+=2;
                        else if( i+2 == len || (path.charAt(i+1) == '.' && path.charAt(i+2) == '/'))
                            i+=3;
                    }else{
                        stack.push('/');
                    while (i < len) {
                        if (path.charAt(i) == '/')
                            break;
                        else 
                            stack.push(path.charAt(i));
                        i++;
                    }
                    }
                    
                    stack.push('/');
                    while (i < len) {
                        if (path.charAt(i) == '/')
                            break;
                        else 
                            stack.push(path.charAt(i));
                        i++;
                    }
                }
                
            }
            len = stack.size();
            if( len == 0)
                return new String("/");
            char[] result = new char[len];
            for (int j = len - 1; j >= 0; j--) {
                result[j] = stack.pop();
            }
            return String.valueOf(result);
    
        }
    }
  • 相关阅读:
    Linux运维笔记
    回到顶部过渡js代码
    好想你红枣
    鼠标点击区域问题
    ie6 hover 子元素无效bug
    IE6和7下text-indent导致inline-block标签消失的bug
    星星评分js代码
    洛谷P3147 [USACO16OPEN]262144 2048 合并 倍增 动归 递推
    洛谷P1114 “非常男女”计划
    洛谷P1108 低价购买 动态规划
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/5958881.html
Copyright © 2020-2023  润新知