天天看点

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

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

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

另一种链表的做法