Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created July 23, 2018 08:38
Show Gist options
  • Save luojiyin1987/f8051396b7185ac38363d4ab70af14a1 to your computer and use it in GitHub Desktop.
Save luojiyin1987/f8051396b7185ac38363d4ab70af14a1 to your computer and use it in GitHub Desktop.
Baseball Game
func calPoints(ops []string) int {
keyStack := make([]int, 0, len(ops))
for i:= range ops{
switch(ops[i]) {
case "+" :
r1 := keyStack[len(keyStack)-1]
r2 := keyStack[len(keyStack)-2]
keyStack = append(keyStack, r1+r2)
case "D":
r1 := keyStack[len(keyStack)-1]
keyStack = append(keyStack , 2* r1)
case "C":
keyStack = keyStack[:len(keyStack)-1]
default:
point , _:= strconv.Atoi(ops[i])
keyStack = append(keyStack, point)
}
}
result := 0
for _, p := range keyStack {
result += p
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment