[Swift]LeetCode71. 簡化路徑 | Simplify Path

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-wkeyociy-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an absolute path for a file (Unix-style), simplify it.git

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"github

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".

 給定一個文檔 (Unix-style) 的徹底路徑,請進行路徑簡化。app

例如,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"ide

邊界狀況:this

    • 你是否考慮了 路徑 = "/../" 的狀況?
      在這種狀況下,你需返回 "/" 。
    • 此外,路徑中也可能包含多個斜槓 '/' ,如 "/home//foo/" 。
      在這種狀況下,你可忽略多餘的斜槓,返回 "/home/foo" 。

 28msspa

 1 class Solution {
 2     func simplifyPath(_ path: String) -> String {
 3         let paths = path.components(separatedBy:"/")
 4         var stack = [String]()
 5         for 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 }

32mscode

 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 }

32mscomponent

 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 }
相關文章
相關標籤/搜索