-
Notifications
You must be signed in to change notification settings - Fork 44
/
_1_game.java
65 lines (62 loc) · 1.68 KB
/
_1_game.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pp.arithmetic.LCP;
/**
* Created by wangpeng on 2019-10-09.
* LCP 1. 猜数字
*
* 小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
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class _1_game {
public static void main(String[] args) {
_1_game game = new _1_game();
System.out.println(game.game(new int[]{1, 2, 3}, new int[]{1, 2, 3}));
System.out.println(game.game(new int[]{2, 2, 3}, new int[]{3, 2, 1}));
}
/**
* 解题思路:
* 最简单的循环比较是否相等即可
*
* @param guess
* @param answer
* @return
*/
public int game(int[] guess, int[] answer) {
int ret = 0;
for (int i = 0; i < guess.length; i++) {
if (guess[i] == answer[i]) {
ret++;
}
}
return ret;
}
}