为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode71. 简化路径 | Simplify Path

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9924010.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

 给定一个文档 (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]()
 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 }

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 }

 

posted @ 2018-11-07 17:41  为敢技术  阅读(352)  评论(0编辑  收藏  举报