This repository was archived by the owner on Sep 20, 2023. It is now read-only.
File tree 1 file changed +9
-29
lines changed
Algorithms/0736.parse-lisp-expression
1 file changed +9
-29
lines changed Original file line number Diff line number Diff line change 1
1
package Problem0736
2
2
3
3
import (
4
- "fmt"
5
4
"strconv"
6
5
"strings"
7
6
)
@@ -35,47 +34,28 @@ func helper(exp string, m map[string]int) int {
35
34
for i = 1 ; i < len (es )- 2 ; i += 2 {
36
35
m [es [i ]] = helper (es [i + 1 ], copy (m ))
37
36
}
38
- // TODO: 删除此处内容
39
- fmt .Println ("\n =======\n " , es [i ])
40
37
return helper (es [i ], copy (m ))
41
38
}
42
39
43
40
}
44
41
45
42
func split (exp string ) []string {
46
- // TODO: 删除此处内容
47
- fmt .Println (exp )
48
43
ss := strings .Split (exp , " " )
49
- leftCount := 0
44
+ countLeft := 0
50
45
res := make ([]string , 0 , len (ss ))
51
46
for _ , s := range ss {
52
-
53
- switch s {
54
- case "(" :
55
- if leftCount == 0 {
56
- res = append (res , s )
57
- } else {
58
- res [len (res )- 1 ] += " ("
59
- }
60
- leftCount ++
61
- case ")" :
62
- res [len (res )- 1 ] += " )"
63
- leftCount --
64
- default :
65
- if leftCount == 0 {
66
- res = append (res , s )
67
- } else {
68
- res [len (res )- 1 ] += " " + s
69
- }
47
+ if countLeft == 0 {
48
+ res = append (res , s )
49
+ } else {
50
+ res [len (res )- 1 ] += " " + s
70
51
}
71
- // TODO: 删除此处内容
72
- fmt .Println (res [len (res )- 1 ])
73
52
74
- if s [ 0 ] == '(' {
75
- leftCount ++
53
+ if s == "(" {
54
+ countLeft ++
76
55
} else if s == ")" {
77
- leftCount --
56
+ countLeft --
78
57
}
58
+
79
59
}
80
60
81
61
return res
You can’t perform that action at this time.
0 commit comments