资讯专栏INFORMATION COLUMN

[LintCode] Simplify Path [字符串操作]

leanxi / 2470人阅读

摘要:,可以用函数去掉所有,然后多考虑一个中间为空的。关于语句的一个特点我们对和其实都是不做操作,然而,两个可以都,但是不能都不做操作。像这样这样这两个就都会等价于下一个就会出错。

Problem

Given an absolute path for a file (Unix-style), simplify it.

Example
"/home/", => "/home" //去掉末尾的slash

"/a/./b/../../c/", => "/c" //每个"/../"对应:删除一个上层的segment
Challenge

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

Note

关于challenge的两点:

"/../",这里讨论的有两种情况,空集和"/../"本身。空集加一个if语句返回slash就可以了,"/../"本身要综合Example的例子,pop出上一层元素。

Multiple slashes,可以用split()函数去掉所有slash,然后多考虑一个slash中间为空的case。

关于switch语句的一个特点:

我们对case ""和case "."其实都是不做操作,然而,两个case可以都break,但是不能都不做操作。像这样:

case "":
case ".":

这样这两个case就都会等价于下一个case:case "..". 就会出错。

Solution
public class Solution {
    public String simplifyPath(String path) {
        Stack stack = new Stack();
        String[] segments = path.split("/");
        for (String segment: segments) {
            switch(segment) {
                case "": break;
                case ".":
                case "..": 
                    if (!stack.isEmpty()) {
                        stack.pop();
                    }
                    break;
                default: stack.push(segment);
            }
        }
        StringBuilder sb = new StringBuilder();
        if (stack.isEmpty()) {//空集的情况
            return "/";
        }
        while (!stack.isEmpty()) {
            sb.insert(0, "/"+stack.pop());//Don"t miss the slash!
        }
        return sb.toString();
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65495.html

相关文章

  • leetcode71. Simplify Path

    摘要:标题文字简化风格的绝对路径。我们可以首先将所有的内容从中分离出来,然后分别处理。这里我们需要用到堆栈的数据结构。堆栈有很多种实现方式,中的类类都可以实现其功能。我们将读到的路径入栈,根据操作符出栈,最后将栈中剩余的元素组织成路径返回即可。 标题文字 Given an absolute path for a file (Unix-style), simplify it. For exa...

    darkerXi 评论0 收藏0
  • [Leetcode] Simplify Path 化简路径

    摘要:栈法复杂度时间空间思路思路很简单,先将整个路径按照分开来,然后用一个栈,遇到时弹出一个,遇到和空字符串则不变,遇到正常路径则压入栈中。注意如果结果为空,要返回一个弹出栈时要先检查栈是否为空代码 Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = /...

    liangzai_cool 评论0 收藏0
  • [LeetCode] 71. Simplify Path

    Problem Given an absolute path for a file (Unix-style), simplify it. For example, path = /home/, => /home path = /a/./b/../../c/, => /c path = /a/../../b/../c//.//, => /c //here: b is cancelle...

    superw 评论0 收藏0
  • 71. Simplify Path

    摘要:题目解答的规则如下三种需要跳过的情况当遇到时,需要向前进出来的顺序是反的,所以加的时候,把最新出来的路径加在前面 题目:Given an absolute path for a file (Unix-style), simplify it. For example,path = /home/, => /homepath = /a/./b/../../c/, => /cclick to ...

    EdwardUp 评论0 收藏0
  • [LintCode/LeetCode] Binary Tree Maximum Path Sum

    摘要:调用函数更新路径和的最大值,而函数本身需要递归,返回的是单边路径和。所以函数要返回的是,主函数中返回的却是最上一层根节点处和的较大值,与之前遍历过所有路径的最大值之间的最大值。 Problem Given a binary tree, find the maximum path sum. The path may start and end at any node in the tre...

    cnTomato 评论0 收藏0

发表评论

0条评论

leanxi

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<