【Question Diary】682. Baseball Game

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

【Question Diary】682. Baseball Game

The eleventh chapter of this diary is titled: 682. Baseball game , simple

1. Topic description:

This question has a lot of text descriptions, but the logic is relatively simple. After slowly brushing a few more questions, we can have a better patience to see the meaning of the question. Although the title is a simple question, we can We can't let it go. Every question will have something worth learning, and there will always be a shining point in everyone.

2. Thought analysis:

1. What idea does this question examine? What is your thinking?

After reading this question carefully, the meaning of the question is still very clear, we need to pay attention to the rules given in the question

The operation logic of "+", "D", "C" and normal integers needs to be handled well , and the others are nothing. The ideas are as follows:

  • Traverse the ops operand array given by the title once. For different characters, we translate them into corresponding numbers, use a help array to record, and use res to record the real-time results.
  • Add up the numbers logically, and finally come to a conclusion

We only need to simulate step by step according to the given characters to simulate the results. There are no hidden precautions. We will demonstrate it again with the example given in the title:

示例1 : ops = ["5","2","C","D","+"]

We can see that the normal simulation is a mathematical problem, pay attention to the special meaning of the three characters C , D , + and the corresponding special logic

Introduced a help array of help records to record our result set

3. Coding

According to the above logic and analysis, we can translate it into the following code, which can be processed according to our mathematical method.

The encoding is as follows:

func calPoints(ops []string) (res int) {
 helper := []int{}
    for _, op := range ops {
        n := len(helper)
        switch op {
        case "+":
            res += helper[n-1] + helper[n-2]
            helper = append(helper, helper[n-1]+helper[n-2])
        case "D":
            res += helper[n-1] * 2
            helper = append(helper, 2*helper[n-1])
        case "C":
            res -= helper[n-1]
            helper = helper[:len(helper)-1]
        default:
            pt, _ := strconv.Atoi(op)
            res += pt
            helper = append(helper, pt)
        }
    }
    return
}
复制代码

It is also very basic to look at the actual encoding, mainly the judgment of characters and the implementation of the corresponding logic. If it is implemented , it is processed on the operation of the array.

4. Summary:

This question is relatively simple, mainly to change the head, the time complexity is O(n), and the space complexity is O(n)

This question gives some very precise definitions and does not let us deal with exceptions

In the actual engineering code, various abnormal situations have to be considered to ensure the robustness of the program. It is necessary to analyze every possibility and scenario, to classify different possible scenarios , sort out, formulate solutions, and output design, code, and more

However, we can start with simple questions and increase our confidence

Original title address: 682. Baseball game

I am here today, what I have learned, if there are any deviations, please correct me

Welcome to like, follow, favorite

Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality

Okay, here it is this time

Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.

I'm the little devil boy Nezha , welcome to like, follow and collect, see you next time~

Guess you like

Origin juejin.im/post/7079597678534852615