[LeetCode]#682. Baseball Game

  1. An integer x - Record a new score of x.
  2. "+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
  3. "D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
  4. "C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
Input: ops = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.
  1. if i is number, add it.
  2. if i is C, remove last one.
  3. if i is D, add ops[i-1]x2
  4. if i is “+”, add ops[i-1] + ops[i-2]
  5. Return Summary
class Solution:
def calPoints(self, ops):
ans=[]

for i in ops:
if i=="C":
ans.pop()
elif i=="D":
ans.append(ans[-1]*2)
elif i=="+":
ans.append(ans[-1]+ans[-2])
else:
ans.append(int(i))
return sum(ans)

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Let’s talk about the new Flote.app

React NextJS Create Instagram UI clone — Header and tooltip Part 9

Quasar App CLI 1.1.0 Released

Vue.js Components Communication Patterns (without Vuex) — Part 5

Custom attributes in HTML correctly

SSR for dynamic meta-tag in React

VRMaker SDK — create your virtual tour!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

How to reverse integer using modulus operator(Python)

LeetCode Patterns Adventure 21 — Minimum Depth of Binary Tree

How to implement a stack class and deque. PYTHON data structures and algorithm.

Easy Level Python Interview Coding Questions.