[Swift]LeetCode71. 簡化路徑 | Simplify Path
阿新 • • 發佈:2018-11-07
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
'/'
"/home//foo/"
.
In this case, you should ignore redundant slashes and return"/home/foo"
.
給定一個文件 (Unix-style) 的完全路徑,請進行路徑簡化。
例如,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
邊界情況:
- 你是否考慮了 路徑 =
"/../"
的情況?
在這種情況下,你需返回"/"
- 此外,路徑中也可能包含多個斜槓
'/'
,如"/home//foo/"
。
在這種情況下,你可忽略多餘的斜槓,返回"/home/foo"
。
28ms
1 class Solution { 2 func simplifyPath(_ path: String) -> String { 3 let paths = path.components(separatedBy:"/") 4 var stack = [String]() 5for str in paths { 6 if str == "." || str == ""{ 7 8 } else if str == ".." { 9 stack.popLast() 10 } else { 11 stack.append(str) 12 } 13 } 14 15 var result = "/" 16 for i in 0..<stack.count { 17 let str = stack[i] 18 if i == stack.count - 1 { 19 result += str 20 } else { 21 result += str + "/" 22 } 23 } 24 25 return result 26 } 27 }
32ms
1 class Solution { 2 func simplifyPath(_ path: String) -> String { 3 let cmpt = path.components(separatedBy: "/") 4 var ret: [String] = [] 5 for item in cmpt { 6 if item == ".." { 7 if ret.count > 0 { 8 ret.removeLast() 9 } 10 } else if item == "." { 11 continue 12 } else if item == "" { 13 continue 14 } else { 15 ret.append(item) 16 } 17 } 18 return "/" + ret.joined(separator: "/") 19 } 20 21 }
32ms
1 class Solution { 2 func simplifyPath(_ path: String) -> String { 3 let arr = path.split{ $0=="/" }.map(String.init) 4 var stack = [String]() 5 for s in arr { 6 if s == "" || s == "." { continue } 7 if s == ".." { _ = stack.popLast(); continue; } 8 stack.append(s) 9 } 10 return stack.isEmpty ? "/" : "/" + stack.joined(separator:"/") 11 } 12 }
40ms
1 class Solution { 2 func simplifyPath(_ path: String) -> String { 3 var components = path.split(separator: "/") 4 var stack = [String.SubSequence]() 5 6 for component in components { 7 if component == "." { 8 continue 9 } else if component == ".." { 10 stack.popLast() 11 } else { 12 stack.append(component) 13 } 14 } 15 16 var output = "/" 17 18 for (index, pathComponent) in stack.enumerated() { 19 if index == stack.count - 1 { 20 output += pathComponent 21 } else { 22 output += pathComponent + "/" 23 } 24 } 25 26 return output 27 } 28 }
48ms
1 class Solution { 2 func simplifyPath(_ path: String) -> String { 3 var myPath = [String]() 4 let presentedPath = path.components(separatedBy: "/") 5 6 for path in presentedPath { 7 if path.count <= 0 {continue} 8 if path == "." { 9 continue 10 } else if path == ".." { 11 if myPath.count > 0 {myPath.removeLast()} 12 } else { 13 myPath.append(path) 14 } 15 } 16 17 var absolutePath = "" 18 19 for path in myPath { 20 absolutePath = absolutePath.count == 0 ? "/\(path)": absolutePath + "/\(path)" 21 } 22 return absolutePath.count > 0 ? absolutePath : "/" 23 } 24 }