Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created July 23, 2018 09:02
Show Gist options
  • Save luojiyin1987/deffbf636532f76c8b2b17340ed14808 to your computer and use it in GitHub Desktop.
Save luojiyin1987/deffbf636532f76c8b2b17340ed14808 to your computer and use it in GitHub Desktop.
Baseball Game
class Solution:
def calPoints(self, ops):
"""
:type ops: List[str]
:rtype: int
"""
n = len(ops)
keyStack = [0 for i in range(n)]
for i in range(n):
if ops[i] == "+":
r1 = keyStack[len(keyStack) -1]
r2 = keyStack[len(keyStack) -2]
keyStack.append(r1+r2)
elif ops[i] == "D":
r1 = keyStack[len(keyStack) -1]
keyStack.append(r1 * 2)
elif ops[i] == "C":
keyStack = keyStack[:len(keyStack)-1]
else:
keyStack.append(int(ops[i]))
return sum(keyStack)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment