[LeetCode] 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path =
path =
path =
"/home/"
, => "/home"
path =
"/a/./b/../../c/"
, => "/c"
Corner Cases:
思路:
用 堆 來解決問題
遇到 .. : stack pop()
遇到 . : pass
遇到 空: pass
遇到 剩下的 : stack append
例子參考 http://www.cnblogs.com/zuoyuan/p/3777289.html
輸入1:
/../a/b/c/./..
輸出1:
/ A / B
模擬整個過程:
1.“/”根目錄
2.“..”跳轉上級目錄,上級目錄為空,所以依舊處於“/”
3.“a”進入子目錄a,目前處於“/ a”
4.“b”進入子目錄b,目前處於“/ a / b”
5.“c”進入子目錄c,目前處於“/ a / b / c”
“”。 當前目錄,不操作,仍處於“/ a / b / c”
7.“..”返回上級目錄,最終為“/ a / b”

- 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"
.
思路:
用 堆 來解決問題
遇到 .. : stack pop()
遇到 . : pass
遇到 空: pass
遇到 剩下的 : stack append
例子參考 http://www.cnblogs.com/zuoyuan/p/3777289.html
輸入1:
/../a/b/c/./..
輸出1:
/ A / B
模擬整個過程:
1.“/”根目錄
2.“..”跳轉上級目錄,上級目錄為空,所以依舊處於“/”
3.“a”進入子目錄a,目前處於“/ a”
4.“b”進入子目錄b,目前處於“/ a / b”
5.“c”進入子目錄c,目前處於“/ a / b / c”
“”。 當前目錄,不操作,仍處於“/ a / b / c”
7.“..”返回上級目錄,最終為“/ a / b”
留言
張貼留言