You're now a baseball game point recorder.
Given a list of strings, each string can be one of the 4 following types:
Integer
(one round's score): Directly represents the number of points you get in this round."+"
(one round's score): Represents that the points you get in this round are the sum of the last twovalid
round's points."D"
(one round's score): Represents that the points you get in this round are the doubled data of the lastvalid
round's points."C"
(an operation, which isn't a round's score): Represents the lastvalid
round's points you get were invalid and should be removed.
Each round's operation is permanent and could have an impact on the round before and the round after.
You need to return the sum of the points you could get in all the rounds.
思路:
题目写了一大堆但是不难理解。给定一个list,list中的"C"表示其前一个数为0,"+" 表示其前两位有效整数的和,"D"表示其前一位有效数字*2.
利用stack,将ops转移到另外一个list,如果遇到"C"则将最后一位,也就是"C"的前一个整数弹出,这样确保"+"和"D"前面都是有效整数。