Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

682. The baseball game

682. Baseball game, simple

I. Title Description:

This text is a little bit more, but logic is relatively simple, slowly brush a few more questions, we can better sit to see the meaning expressed by the title, although the title is a simple question, but we can’t let go, each question, there will be the place where we are worth learning, each person also there will always be shine

Ii. Analysis of Ideas:

1. What ideas does this question examine? What’s your thinking?

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

The logic for “+”, “D”, “C”, and normal integers needs to be handled well.

  • The ops operation number set given in the topic is traversed once. For different characters, we translate them into corresponding numbers, record them with a help array, and record the real-time results with RES
  • Add up the numbers logically and come to a conclusion

We just need to follow the given character step by step simulation can simulate the result, there is no hidden note, let’s use the example given in the question to demonstrate:

Example 1: OPS = [“5″,”2″,”C”,”D”,”+”]

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

A help array was introduced to record our result set

Three, coding

According to the above logic and analysis, we can translate into the following code, according to our mathematical way to deal with

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[n2 -]
            helper = append(helper, helper[n- 1]+helper[n2 -])
        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
}
Copy the code

See the actual code is also very basic, mainly on the character of the judgment, and the implementation of the corresponding logic, the implementation is in the array operation above processing

Iv. Summary:

It’s a simple case, it’s just a change of head, it’s order n in time, order n in space.

They give us some very precise definitions and don’t ask us to deal with exceptions

In the actual engineering code, we have to consider all kinds of abnormal situations to ensure the robustness of the program. We need to analyze every possibility and scenario to classify and unify different possible scenarios, formulate solutions, output design and coding, etc

But we can start with easy questions to build confidence

682. Baseball game

Today is here, learning, if there is a deviation, please correct

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

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

I am Nezha, welcome to like, see you next time ~