File tree Expand file tree Collapse file tree 3 files changed +54
-3
lines changed
LeetCodePlayground.playground/Pages/71_SimplifyPath.xcplaygroundpage Expand file tree Collapse file tree 3 files changed +54
-3
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // 71. 简化路径
3
+ //
4
+ // 题目链接:https://leetcode-cn.com/problems/simplify-path/
5
+ // 标签:栈、字符串
6
+ // 要点:根据题意,考虑清楚出入栈条件,以及跳过逻辑
7
+ // 时间复杂度:O(N)
8
+ // 空间复杂度:O(N)
9
+ //
10
+
11
+ import Foundation
12
+
13
+ class Solution {
14
+ func simplifyPath( _ path: String ) -> String {
15
+ let paths = path. split ( separator: " / " )
16
+
17
+ var pathStack = [ String] ( )
18
+
19
+ for path in paths {
20
+ guard path != " . " , !path. isEmpty else { continue }
21
+
22
+ if path == " .. " {
23
+ pathStack. popLast ( )
24
+ } else {
25
+ pathStack. append ( String ( path) )
26
+ }
27
+ }
28
+
29
+ return " / " + pathStack. joined ( separator: " / " )
30
+ }
31
+ }
32
+
33
+ // Tests
34
+ let s = Solution ( )
35
+ s. simplifyPath ( " /home/ " ) == " /home "
36
+ s. simplifyPath ( " /../ " ) == " / "
37
+ s. simplifyPath ( " /home//foo/ " ) == " /home/foo "
38
+ s. simplifyPath ( " /a/./b/../../c/ " ) == " /c "
39
+ s. simplifyPath ( " /a/../../b/../c//.// " ) == " /c "
40
+ s. simplifyPath ( " /a//b////c/d//././/.. " ) == " /a/b/c "
Original file line number Diff line number Diff line change 1
1
//: [Previous](@previous)
2
2
3
+ //
4
+ // 71. 简化路径
5
+ //
6
+ // 题目链接:https://leetcode-cn.com/problems/simplify-path/
7
+ // 标签:栈、字符串
8
+ // 要点:根据题意,考虑清楚出入栈条件,以及跳过逻辑
9
+ // 时间复杂度:O(N)
10
+ // 空间复杂度:O(N)
11
+ //
12
+
3
13
import Foundation
4
14
5
15
class Solution {
Original file line number Diff line number Diff line change @@ -54,9 +54,10 @@ LeetCode 💖 Swift,攻克[数据结构](#数据结构)与[算法](#算法)。
54
54
55
55
### 栈
56
56
57
- | 标题 | 标签 | 描述 | 题解 |
58
- | :----------------------------------------------------------: | :-----------: | ------------------------------ | :--------------------------------------------------------: |
59
- | 1003. [ 检查替换后的词是否有效] ( https://leetcode-cn.com/problems/check-if-word-is-valid-after-substitutions/ ) | ` 字符串 ` ` 栈 ` | 按给定规则检查替换后的词有效性 | [ Swift] ( ./1003_CheckIfWordIsValidAfterSubstitutions.swift ) |
57
+ | 标题 | 标签 | 描述 | 题解 |
58
+ | :----------------------------------------------------------: | :-----------: | ------------------------------------------------------------ | :--------------------------------------------------------: |
59
+ | 1003. [ 检查替换后的词是否有效] ( https://leetcode-cn.com/problems/check-if-word-is-valid-after-substitutions/ ) | ` 字符串 ` ` 栈 ` | 按给定规则检查替换后的词有效性 | [ Swift] ( ./1003_CheckIfWordIsValidAfterSubstitutions.swift ) |
60
+ | [ 71. 简化路径] ( https://leetcode-cn.com/problems/simplify-path/ ) | ` 字符串 ` ` 栈 ` | 以 Unix 风格给出一个文件的** 绝对路径** ,你需要简化它。或者换句话说,将其转换为规范路径。 | [ Swift] ( ./71_SimplifyPath.swift ) |
60
61
61
62
### 队列
62
63
You can’t perform that action at this time.
0 commit comments