題目
原題頁面:https://leetcode.com/problems/simplify-path/
題目型別:Stack, String
難度評價:Medium
Given an absolute path for a file (Unix-style), simplify it.
For example,
path ="/home/", =>"/home"
path ="/a/./b/../../c/", =>"/c"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".
分析
用棧分析Linux路徑,得出最後的結果。需要注意的就是Corner Cases了,如果是Windows用的相對較多的同學,很可能就不會注意到這些細節。但實際上很多涉及路徑的win32 API也支援這種Corner Cases。
程式碼
1 | class Solution: |
