題目:小A 和 小B 在玩猜數字,小B 每次從 1, 2, 3 中隨機選擇一個,小A 每次也從 1, 2, 3 中選擇一個猜,他們一共進行三次這個游戲,請回傳 小A 猜對了幾次?
輸入的guess陣列為 小A 每次的猜測,answer陣列為 小B 每次的選擇,guess和answer的長度都等于3,
示例 1:
輸入:guess = [1,2,3], answer = [1,2,3]
輸出:3
解釋:小A 每次都猜對了,
示例 2:
輸入:guess = [2,2,3], answer = [3,2,1]
輸出:1
解釋:小A 只猜對了第二次,
限制:
guess的長度 = 3
answer的長度 = 3
guess的元素取值為 {1, 2, 3} 之一,
answer的元素取值為 {1, 2, 3} 之一,
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/guess-numbers
著作權歸領扣網路所有,商業轉載請聯系官方授權,非商業轉載請注明出處,
解題思路:
1.首先設定一個變數times,
2.用if條件陳述句判斷,
Python3的代碼:
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
運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/189674.html
標籤:其他
下一篇:re模塊常用方法
