Created
July 23, 2018 09:02
-
-
Save luojiyin1987/deffbf636532f76c8b2b17340ed14808 to your computer and use it in GitHub Desktop.
Baseball Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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