[LeetCode]--71. Simplify Path

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”.

这个题目重点就是要理解它的意思,如果是一个点 . 那就是当前路径,不管,如果是两个点 .. 那就是当前路径的上一个目录。这样我们用栈来表示的话,就是如下所示

path:"/a/./b/../../c/"

split:"a",".","b","..","..","c"

stack:push(a), push(b), pop(b), pop(a), push(c) --> c

明白这个之后就一目了然了,就是注意返回的时候如果是”/”或者”/../”这种情形就行。

public String simplifyPath(String path) {
        String res = "";
        String[] arrs = path.split("/");
        Stack<String> s = new Stack<String>();

        for (int i = 0; i < arrs.length; i++) {
            if (arrs[i].equals("")) {
                continue;
            }
            if (!arrs[i].equals(".") && !arrs[i].equals("..")) {
                s.push(arrs[i]);
            }
            if (arrs[i].equals("..") && !s.isEmpty()) {
                s.pop();
            }
        }
        if (s.isEmpty())
            return "/";
        while (!s.isEmpty())
            res = "/" + s.pop() + res;
        return res;
    }

另一种链表的做法

public String simplifyPath1(String path) {
        String result = "/";
        String[] stubs = path.split("/+");
        ArrayList<String> paths = new ArrayList<String>();
        for (String s : stubs){
            if(s.equals("..")){
                if(paths.size() > 0){
                    paths.remove(paths.size() - 1);
                }
            }
            else if (!s.equals(".") && !s.equals("")){
                paths.add(s);
            }
        }
        for (String s : paths){
            result += s + "/";
        }
        if (result.length() > 1)
            result = result.substring(0, result.length() - 1);
        return result;
    }
时间: 2024-08-04 18:55:47

[LeetCode]--71. Simplify Path的相关文章

[LeetCode] Longest Univalue Path 最长相同值路径

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

Simplify Path:LeetCode

原题链接: http://oj.leetcode.com/problems/simplify-path/ 这道题目是Linux内核中比较常见的一个操作,就是对一个输入的文件路径进行简化.思路比较明确,就是维护一个栈,对于每一个块(以'/'作为分界)进行分析,如果遇到'../'则表示要上一层,那么就是进行出栈操作,如果遇到'./'则是停留当前,直接跳过,其他文件路径则直接进栈即可.最后根据栈中的内容转换成路径即可(这里是把栈转成数组,然后依次添加).时间上不会超过两次扫描(一次是进栈得到简化路径,

[LeetCode]64.Minimum Path Sum

[题目] Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. [思路] 设sum[i][j] 到位置(i,j)时路径最

Simplify Path

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

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

CI框架源码阅读笔记2 一切的入口 index.php

上一节(CI框架源码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程,这里再次贴出流程图,以备参考: 作为CI框架的入口文件,源码阅读,自然由此开始.在源码阅读的过程中,我们并不会逐行进行解释,而只解释核心的功能和实现. 1. 设置应用程序环境 define("ENVIRONMENT", "development"); 这里的development可以是任何你喜欢的环境名称(比如dev,再如test),相对应的,你要在下面的switch

iOS开发:Unity3D 使用C#语言建立本地数据库

  首先你需要得到Mono.Data.Sqlite.dll 文件 与System.Data.dll文件.如果你在Mac 操作系统下使用Unity那么很悲剧,找不到这两个文件,至少我没能找到.后来我在Windows下的Unity安装路径中找到了它.为了方便大家我将这两个文件上传至网盘中,如果没有这两个文件的朋友请下载.Unity数据库文件.zip .zip文件下载完毕后直接解压,然后将Mono.Data.Sqlite.dll 文件 与System.Data.dll文件放在Unity工程中的Asse