[LeetCode&Python] Problem 682. Baseball Game

You‘re now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

  1. Integer (one round‘s score): Directly represents the number of points you get in this round.
  2. "+" (one round‘s score): Represents that the points you get in this round are the sum of the last two valid round‘s points.
  3. "D" (one round‘s score): Represents that the points you get in this round are the doubled data of the last valid round‘s points.
  4. "C" (an operation, which isn‘t a round‘s score): Represents the last valid 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.

Example 1:

Input: ["5","2","C","D","+"]
Output: 30
Explanation:
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get 2 points. The sum is: 7.
Operation 1: The round 2‘s data was invalid. The sum is: 5.
Round 3: You could get 10 points (the round 2‘s data has been removed). The sum is: 15.
Round 4: You could get 5 + 10 = 15 points. The sum is: 30.

Example 2:

Input: ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get -2 points. The sum is: 3.
Round 3: You could get 4 points. The sum is: 7.
Operation 1: The round 3‘s data is invalid. The sum is: 3.
Round 4: You could get -4 points (the round 3‘s data has been removed). The sum is: -1.
Round 5: You could get 9 points. The sum is: 8.
Round 6: You could get -4 + 9 = 5 points. The sum is 13.
Round 7: You could get 9 + 5 = 14 points. The sum is 27.

Note:

  • The size of the input list will be between 1 and 1000.
  • Every integer represented in the list will be between -30000 and 30000.

Just use a stack to solve this problem.

class Solution:
    def calPoints(self, ops):
        """
        :type ops: List[str]
        :rtype: int
        """
        ans=0
        baseballstack=collections.deque()

        for c in ops:
            if c!=‘C‘ and c!=‘D‘ and c!=‘+‘:
                num=int(c)
                ans+=num
                baseballstack.appendleft(num)
            elif c==‘C‘:
                num=baseballstack.popleft()
                ans-=num
            elif c==‘D‘:
                num=baseballstack[0]
                num*=2
                ans+=num
                baseballstack.appendleft(num)
            elif c==‘+‘:
                num=baseballstack[0]
                num2=baseballstack[1]
                num+=num2
                ans+=num
                baseballstack.appendleft(num)

        return ans

  

原文地址:https://www.cnblogs.com/chiyeung/p/9771984.html

时间: 2024-07-31 22:22:42

[LeetCode&Python] Problem 682. Baseball Game的相关文章

[LeetCode&Python] Problem 905: Sort Array By Parity

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. You may return any answer array that satisfies this condition. Example 1: Input: [3,1,2,4] Output: [2,4,3,1] T

[LeetCode&Python] Problem 806. Number of Lines To Write String

We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an arra

[LeetCode&Python] Problem 811. Subdomain Visit Count

A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit

[LeetCode&Python] Problem 520. Detect Capital

Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases holds: All letters in this word are capitals, like "USA". All letters in t

[LeetCode&Python] Problem 427. Construct Quad Tree

We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it repres

[LeetCode&Python] Problem 771: Jewels and Stones

You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in Sis a type of stone you have.  You want to know how many of the stones you have are also jewels. The letters in J are

[LeetCode&Python] Problem 832. Flipping an Image

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0,

[LeetCode&Python] Problem 728. Self Dividing Numbers

A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not allowed to contain the digit zero. G

[LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal

Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary tree: Return its postorder traversal as: [5,6,3,2,4,1]. Note: Recursive solution is trivial, could you do it iteratively? Recursion Solution: ""