We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 67f5f0e commit 47cb38fCopy full SHA for 47cb38f
Algorithms/0071.simplify-path/simplify-path.go
@@ -4,10 +4,12 @@ import "strings"
4
5
func simplifyPath(path string) string {
6
lp := len(path)
7
- stack := []string{}
8
- dir := []byte{}
+ stack := make([]string, 0, lp/2)
+ dir := make([]byte, 0, lp)
9
10
for i := 0; i < lp; i++ {
11
+ // 使用前,清空 dir
12
+ dir = dir[:0]
13
// 截取 dir
14
for i < lp && path[i] != '/' {
15
dir = append(dir, path[i])
@@ -25,10 +27,7 @@ func simplifyPath(path string) string {
25
27
}
26
28
default:
29
stack = append(stack, s)
-
30
31
- dir = dir[:0]
32
33
34
return "/" + strings.Join(stack, "/")
0 commit comments