LeetCode LCP 1. Guess the number

LeetCode LCP 1. Guess the number

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • 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 and guess how many times it is right? The input guessarray is small A’s guess every time, and the answerarray is small B’s choice every time. guessAnd answera length equal to 3.
  • Example :
示例 1 :
输入:guess = [1,2,3], answer = [1,2,3]
输出:3
解释:小A 每次都猜对了。
示例 2 :
输入:guess = [2,2,3], answer = [3,2,1]
输出:1
解释:小A 只猜对了第二次。
  • Restrictions :
    1. guesslength = 3
    2. answerlength = 3
    3. guessThe element value of {1, 2, 3}one.
    4. answerThe element value of {1, 2, 3}one.
  • Code:
class Solution:
    def game(self, guess: List[int], answer: List[int]) -> int:
        count = 0
        for i in range(3):
            if guess[i] == answer[i]:
                count += 1
        return count
# 执行用时 :28 ms, 在所有 Python3 提交中击败了100.00%的用户
# 内存消耗 :13.8 MB, 在所有 Python3 提交中击败了100.00%的用户
  • Algorithm description:
    directly judge the elements at the corresponding positions, if they are equal, guess the number of times +1, and return the final result.

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/106627157