HTMLify
LeetCode - Baseball Game - Go
Views: 308 | Author: abh
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import "fmt" type Stack struct { values []int } func (stack *Stack) push(n int) { stack.values = append(stack.values, n) } func (stack *Stack) pop() { stack.values = stack.values[:len(stack.values)-1] } func sti(s string) int { var i int n := false if s[0] == '-' { n = true i++ } var num int for ;i<len(s); i++ { num = (num*10)+int(s[i])-48 } if n { return -num } return num } func is_numder(s string) bool{ for _, c := range s { if !((47 < c && c < 58) || c == '-') { return false } } return true } func calPoints(operations []string) int { var stack Stack for _, o := range operations { if is_numder(o) { n := sti(o) stack.push(n) } if o == "+" { l := len(stack.values) stack.push(stack.values[l-2]+stack.values[l-1]) } if o == "D" { stack.push(stack.values[len(stack.values)-1]*2) } if o == "C" { stack.pop() } } var ans int for _, v := range stack.values { ans += v } fmt.Println(stack.values) return ans } |