LeetCode has one question every day! ! (LCP 01. Guess the number)

Topic : Little A and Little B are playing guessing numbers. Little B randomly chooses one from 1, 2, 3 each time, and Little A also chooses one guess from 1, 2, 3 each time. They played this game three times in total. Please go back to Little A. How many times did you guess correctly?

The input guess array is the guess of small A each time, and the answer array is the choice of small B each time. The length of guess and answer is equal to 3.

Example 1:

Input: guess = [1,2,3], answer = [1,2,3]
Output: 3
Explanation: Little A guessed right every time.

Example 2:

Input: guess = [2,2,3], answer = [3,2,1]
Output: 1
Explanation: Little A only guessed correctly the second time.

limit:

The length of guess = 3
The length of answer = 3
The element of guess is one of {1, 2, 3}.
The element of answer is one of {1, 2, 3}.

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/guess-numbers
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Problem-solving ideas :
1. First set a variable times.
2. Use if conditional statement to judge.
Python3 code :

class Solution:
    def game(self, guess: List[int], answer: List[int]) -> int:
        times = 0
        for i in range(len(answer)):
            if guess[i] == answer[i]:
                times += 1
            else:
                pass
        return times            

Operation result :
Insert picture description here

Guess you like

Origin blog.csdn.net/Kinght_123/article/details/109225087