File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
LeetCodePlayground.playground
Pages/71_SimplifyPath.xcplaygroundpage Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ //: [Previous](@previous)
2
+
3
+ import Foundation
4
+
5
+ class Solution {
6
+ func simplifyPath( _ path: String ) -> String {
7
+ let paths = path. split ( separator: " / " )
8
+
9
+ var pathStack = [ String] ( )
10
+
11
+ for path in paths {
12
+ guard path != " . " , !path. isEmpty else { continue }
13
+
14
+ if path == " .. " {
15
+ pathStack. popLast ( )
16
+ } else {
17
+ pathStack. append ( String ( path) )
18
+ }
19
+ }
20
+
21
+ return " / " + pathStack. joined ( separator: " / " )
22
+ }
23
+ }
24
+
25
+ // Tests
26
+ let s = Solution ( )
27
+ s. simplifyPath ( " /home/ " ) == " /home "
28
+ s. simplifyPath ( " /../ " ) == " / "
29
+ s. simplifyPath ( " /home//foo/ " ) == " /home/foo "
30
+ s. simplifyPath ( " /a/./b/../../c/ " ) == " /c "
31
+ s. simplifyPath ( " /a/../../b/../c//.// " ) == " /c "
32
+ s. simplifyPath ( " /a//b////c/d//././/.. " ) == " /a/b/c "
33
+
34
+ //: [Next](@next)
Original file line number Diff line number Diff line change 22
22
<page name =' 75_SortColors' />
23
23
<page name =' 215_FindKthLargestInAnArray' />
24
24
<page name =' 88_MergeSortedArray' />
25
+ <page name =' 71_SimplifyPath' />
25
26
</pages >
26
27
</playground >
You can’t perform that action at this time.
0 commit comments