Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a77d62

Browse files
猜数字
1 parent e9db6f5 commit 0a77d62

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.leetcode_cn.easy;
2+
/***************猜数字*************/
3+
4+
/**
5+
* 小A 和 小B 在玩猜数字。小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜。
6+
* 他们一共进行三次这个游戏,请返回 小A 猜对了几次?
7+
*
8+
*
9+
*
10+
* 输入的guess数组为 小A 每次的猜测,answer数组为 小B 每次的选择。guess和answer的长度都等于3。
11+
*
12+
*
13+
*
14+
* 示例 1:
15+
* 输入:guess = [1,2,3], answer = [1,2,3]
16+
* 输出:3
17+
* 解释:小A 每次都猜对了。
18+
*
19+
*
20+
* 示例 2:
21+
* 输入:guess = [2,2,3], answer = [3,2,1]
22+
* 输出:1
23+
* 解释:小A 只猜对了第二次。
24+
*
25+
*
26+
* 限制:
27+
*
28+
* guess的长度 = 3
29+
* answer的长度 = 3
30+
* guess的元素取值为 {1, 2, 3} 之一。
31+
* answer的元素取值为 {1, 2, 3} 之一。
32+
*
33+
*/
34+
public class GuessNumbers {
35+
36+
public int game(int[] guess, int[] answer) {
37+
int result = 0;
38+
for (int i = 0; i < 3; i++) {
39+
if (guess[i] == answer[i])
40+
result++;
41+
}
42+
return result;
43+
}
44+
45+
public static void main(String[] args) {
46+
System.out.println(2 ^ 2); // 异或 语法 相同输出 0
47+
}
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /