天天看點

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

這個題目重點就是要了解它的意思,如果是一個點 . 那就是目前路徑,不管,如果是兩個點 .. 那就是目前路徑的上一個目錄。這樣我們用棧來表示的話,就是如下所示

明白這個之後就一目了然了,就是注意傳回的時候如果是”/”或者”/../”這種情形就行。

另一種連結清單的做法