成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

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

leanxi / 2673人閱讀

摘要:,可以用函數(shù)去掉所有,然后多考慮一個中間為空的。關(guān)于語句的一個特點我們對和其實都是不做操作,然而,兩個可以都,但是不能都不做操作。像這樣這樣這兩個就都會等價于下一個就會出錯。

Problem

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

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

"/a/./b/../../c/", => "/c" //每個"/../"對應(yīng):刪除一個上層的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

關(guān)于challenge的兩點:

"/../",這里討論的有兩種情況,空集和"/../"本身。空集加一個if語句返回slash就可以了,"/../"本身要綜合Example的例子,pop出上一層元素。

Multiple slashes,可以用split()函數(shù)去掉所有slash,然后多考慮一個slash中間為空的case。

關(guān)于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();
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65495.html

相關(guān)文章

  • leetcode71. Simplify Path

    摘要:標(biāo)題文字簡化風(fēng)格的絕對路徑。我們可以首先將所有的內(nèi)容從中分離出來,然后分別處理。這里我們需要用到堆棧的數(shù)據(jù)結(jié)構(gòu)。堆棧有很多種實現(xiàn)方式,中的類類都可以實現(xiàn)其功能。我們將讀到的路徑入棧,根據(jù)操作符出棧,最后將棧中剩余的元素組織成路徑返回即可。 標(biāo)題文字 Given an absolute path for a file (Unix-style), simplify it. For exa...

    darkerXi 評論0 收藏0
  • [Leetcode] Simplify Path 化簡路徑

    摘要:棧法復(fù)雜度時間空間思路思路很簡單,先將整個路徑按照分開來,然后用一個棧,遇到時彈出一個,遇到和空字符串則不變,遇到正常路徑則壓入棧中。注意如果結(jié)果為空,要返回一個彈出棧時要先檢查棧是否為空代碼 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

    摘要:題目解答的規(guī)則如下三種需要跳過的情況當(dāng)遇到時,需要向前進(jìn)出來的順序是反的,所以加的時候,把最新出來的路徑加在前面 題目: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

    摘要:調(diào)用函數(shù)更新路徑和的最大值,而函數(shù)本身需要遞歸,返回的是單邊路徑和。所以函數(shù)要返回的是,主函數(shù)中返回的卻是最上一層根節(jié)點處和的較大值,與之前遍歷過所有路徑的最大值之間的最大值。 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

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<