-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path71.simplifyPath.go
42 lines (35 loc) · 1.06 KB
/
71.simplifyPath.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package mystring
import (
"fmt"
"path/filepath"
"strings"
"Leetcode/algorithms/kit"
)
// 1. 用栈
// 2. 先把字符串以"/"为分隔符分割成数组,此数组包含"路径字符串"、""、"."、".."这四种情况;
// 3. 遍历数组, 当s[i]==".."并且栈不空时pop, 当s[i]!="" && s[i]!="." && s[i]!=".."时,把s[i]路径压入栈;
// 4. 如果栈空, 返回"/", 栈非空, 从栈底部(注意不是栈顶)开始拼接字符串
func simplifyPath(path string) string {
stack := kit.NewStack()
parts := strings.Split(path, "/")
fmt.Printf("origin:%s split parts:%v\n", path, parts)
for _, part := range parts {
if !stack.IsEmpty() && part == ".." {
stack.Pop()
} else if part != "" && part != "." && part != ".." {
stack.Push(part)
}
}
if stack.IsEmpty() {
return "/"
}
var items []string
for i := 0; i < stack.Len(); i++ {
items = append(items, (stack.Seek(i)).(string))
}
return "/" + strings.Join(items, "/")
}
// golang标准库提供了牛逼的此方法
func cleanFilepath(path string) string {
return filepath.Clean(path)
}