1. 程式人生 > >LeetCode 71. Simplify Path(簡化路徑)

LeetCode 71. Simplify Path(簡化路徑)

題目描述

給定一個文件 (Unix-style) 的完全路徑,請進行路徑簡化。

例如,

path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

邊界情況:

  • 你是否考慮了 路徑 = “/../” 的情況?
    在這種情況下,你需返回 “/” 。
  • 此外,路徑中也可能包含多個斜槓 ‘/’ ,如 “/home//foo/” 。
    在這種情況下,你可忽略多餘的斜槓,返回 “/home/foo” 。

分析

思路其實很簡單,切割字串後,取出每一個路徑名,如果是空,跳過;如果路徑中沒有出現.,那麼壓入棧;如果包含.,就看有幾個.,一個表示當前目錄,無意義,三個以上根據題目要求也要壓入棧。只有..

一種情況需要pop,在pop之前還要檢查棧中是否有元素,沒有元素也不需要進行操作。最後將剩餘的路徑用/連線起來即可。

public String simplifyPath(String path) {

    Stack<String> stack = new Stack<String>();
    for (String s : path.split("/")) {
        if (s.isEmpty())
            continue;

        if (s.contains(".")) {
            if (s.equals
(".")) { continue; } if (s.equals("..")) { if (stack.isEmpty()) continue; stack.pop(); } else { stack.push(s); } } else { stack.push(s); } } StringBuilder sb =
new StringBuilder(); for (String s : stack) { sb.append("/" + s); } return sb.length() == 0 ? "/" : sb.toString(); }

以下是shpolsky的解決方法,與我的思路是相同的,但很簡潔。

public String simplifyPath(String path) {
    Deque<String> stack = new LinkedList<>();
    Set<String> skip = new HashSet<>(Arrays.asList("..",".",""));
    for (String dir : path.split("/")) {
        if (dir.equals("..") && !stack.isEmpty()) stack.pop();
        else if (!skip.contains(dir)) stack.push(dir);
    }
    String res = "";
    for (String dir : stack) res = "/" + dir + res;
    return res.isEmpty() ? "/" : res;
}

如果文章裡有說得不對的地方請前輩多多指正~ 與君共勉~

相關推薦

LeetCode 71. Simplify Path簡化路徑

題目描述 給定一個文件 (Unix-style) 的完全路徑,請進行路徑簡化。 例如, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" 邊界情況: 你是否考慮

leetcode 71. Simplify Path【檔案路徑表示式】

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

LeetCode 71. Simplify Path 求絕對路徑,字串處理題

字串 處理題注意以下幾點即可:     ////  即連續的/    /...    則...是目錄名或檔名,而非..和.   空的輸入,為/class Solution { public: s

LeetCode71. Simplify PathC++

地址:https://leetcode.com/problems/simplify-path/ 題目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/

LeetCode---71. Simplify Path

LeetCode—71. Simplify Path 題目 思路及解法 分隔符"/“之間的字元可以分為三種情況,根據這三種情況我們使用棧實現操作: 1.如果是”…",應該返回上級選單,對應出棧 2.如果是".“或者”",保持不變,不對棧進行操作 3.其他情況將

Leetcode 71 Simplify Path

Given an absolute path for a file (Unix-style), simplify it.  For example,path = "/home/", => "/home"path = "/a/.

LeetCode-71-Simplify Path

tin evel col 問題 ack see stream other pla 算法描述: Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it

19.2.13 [LeetCode 71] Simplify Path

res 根目錄 要求 targe display alt fin per bsp Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to th

Leetcode 71 簡化路徑simplify-path

font 出錯 標準 多個 push ac代碼 -s cpp mark 給定一個文檔 (Unix-style) 的完全路徑,請進行路徑簡化。 例如,path = "/home/", => "/home"path = "/a/./b/../../c/", => "

Leetcode 71 簡化路徑simplify-path

給定一個文件 (Unix-style) 的完全路徑,請進行路徑簡化。 例如, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” 邊界情況: 你是否考慮了 路徑 = “

LeetCode-面試算法經典-Java實現】【062-Unique Paths唯一路徑

ade ssi comment span there sdn href func 圖片 【062-Unique Paths(唯一路徑)】 【LeetCode-面試算法經典-Java實現】【全部題目文件夾索引】 原題   A robot is

LeetCode算法系列:71. Simplify Path

目錄 題目描述 演算法實現 題目描述 Given an absolute path for a file (Unix-style), simplify it.  For example,path = "/home/", => "/home"path =

Mindmanager 安裝過程中 ERROR 1320. The specified path is too long 檔案路徑 等問題

    電腦擁有的時間長了總是想重新裝一下,或是體驗那種不一樣的心情,或是重新整理自己電腦。其中也包括我,這不重灌之後mindmanager 安裝出現問題了。 第一個遇到的困難就是安裝失敗,在安裝的時候總是提示出現"ERROR 1320. The specified pat

LeetCode | Unique Paths唯一路徑

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or

【九度】題目1090:路徑列印 && 【LeetCodeSimplify Path

時間限制:1 秒記憶體限制:32 兆特殊判題:否提交:1319解決:230 題目描述: 給你一串路徑,譬如: a\b\c a\d\e b\cst d\ 你把這些路徑中蘊含的目錄結構給畫出來,子目錄直接列在父目錄下面,並比父目錄向右縮一格,就像這樣: a   b     c

LeetCode 223 Rectangle Area矩形面積

lee return common 分享 span spa class tracking mod 翻譯 找到在二維平面中兩個相交矩形的總面積。 每一個矩形都定義了其左下角和右上角的坐標。 (矩形例如以下圖) 如果,總占地面積永遠不會超過int

71. Simplify Path

實現 push 需要 sta ash 裏的 light als brush https://leetcode.com/problems/simplify-path/#/description 難度:85,雖然不難,但是裏面關於LinkedList實現的棧的各種技巧的使用,還

LeetCode 54. Spiral Matrix螺旋矩陣

gin 每一個 owin code -1 new 完成 length tco Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral

LeetCode 55. Jump Game 跳躍遊戲

mat col lean osi pub 情況 you track rip Given an array of non-negative integers, you are initially positioned at the first index of the ar

LeetCode 66. Plus One加1

class 數字 public store res rest self present [0 Given a non-negative integer represented as a non-empty array of digits, plus one to the i