样例 1

``````输入：
guess = [1,2,3], answer = [1,2,3]

3

``````

样例 2

``````输入：
guess = [2,2,3], answer = [3,2,1]

1

``````

限制

• guess 的长度 = 3
• guess 的元素取值为 {1, 2, 3} 之一。
• answer 的元素取值为 {1, 2, 3} 之一。

分析

• 这道题选不到rust语言。
• 这道题很简单，但是总觉得可能有什么地方能优化。

题解

java

``````class Solution {

public int game(int[] guess, int[] answer) {

return (guess[0] == answer[0] ? 1 : 0) + (guess[1] == answer[1] ? 1 : 0) + (guess[2] == answer[2] ? 1 : 0);
}
}
``````

c

``````int game(int* guess, int guessSize, int* answer, int answerSize){

return (guess[0] == answer[0] ? 1 : 0) + (guess[1] == answer[1] ? 1 : 0) + (guess[2] == answer[2] ? 1 : 0);
}
``````

c++

``````class Solution {

public:
int game(vector<int>& guess, vector<int>& answer) {

return (guess[0] == answer[0] ? 1 : 0) + (guess[1] == answer[1] ? 1 : 0) + (guess[2] == answer[2] ? 1 : 0);
}
};
``````

python

``````class Solution:
def game(self, guess: List[int], answer: List[int]) -> int:
return sum(guess[i] == answer[i] for i in range(3))
``````

go

``````func game(guess []int, answer []int) int {

ans := 0

ans++
}

ans++
}

ans++
}
return ans
}
``````

原题传送门：https://leetcode-cn.com/problems/guess-numbers/

https://blog.csdn.net/leyi520/article/details/120956756