【LeetCode】299. Bulls and Cows 猜数字游戏(Medium)(JAVA)
题目地址: https://leetcode.com/problems/bulls-and-cows/
题目描述:
You are playing the Bulls and Cows game with your friend.
You write down a secret number and ask your friend to guess what the number is. When your friend makes a guess, you provide a hint with the following info:
- The number of “bulls”, which are digits in the guess that are in the correct position.
- The number of “cows”, which are digits in the guess that are in your secret number but are located in the wrong position. Specifically, the non-bull digits in the guess that could be rearranged such that they become bulls.
Given the secret number secret and your friend’s guess guess, return the hint for your friend’s guess.
The hint should be formatted as “xAyB”, where x is the number of bulls and y is the number of cows. Note that both secret and guess may contain duplicate digits.
Example 1:
Input: secret = "1807", guess = "7810"
Output: "1A3B"
Explanation: Bulls are connected with a '|' and cows are underlined:
"1807"
|
"7810"
Example 2:
Input: secret = "1123", guess = "0111"
Output: "1A1B"
Explanation: Bulls are connected with a '|' and cows are underlined:
"1123" "1123"
| or |
"0111" "0111"
Note that only one of the two unmatched 1s is counted as a cow since the non-bull digits can only be rearranged to allow one 1 to be a bull.
Example 3:
Input: secret = "1", guess = "0"
Output: "0A0B"
Example 4:
Input: secret = "1", guess = "1"
Output: "1A0B"
Constraints:
- 1 <= secret.length, guess.length <= 1000
- secret.length == guess.length
- secret and guess consist of digits only.
题目大意
你在和朋友一起玩 猜数字(Bulls and Cows)游戏,该游戏规则如下:
- 你写出一个秘密数字,并请朋友猜这个数字是多少。
- 朋友每猜测一次,你就会给他一个提示,告诉他的猜测数字中有多少位属于数字和确切位置都猜对了(称为“Bulls”, 公牛),有多少位属于数字猜对了但是位置不对(称为“Cows”, 奶牛)。
- 朋友根据提示继续猜,直到猜出秘密数字。
请写出一个根据秘密数字和朋友的猜测数返回提示的函数,返回字符串的格式为 xAyB ,x 和 y 都是数字,A 表示公牛,用 B 表示奶牛。
- xA 表示有 x 位数字出现在秘密数字中,且位置都与秘密数字一致。
- yB 表示有 y 位数字出现在秘密数字中,但位置与秘密数字不一致。
请注意秘密数字和朋友的猜测数都可能含有重复数字,每位数字只能统计一次。
说明: 你可以假设秘密数字和朋友的猜测数都只包含数字,并且它们的长度永远相等。
解题方法
- 首先遍历一遍判断出所有数字相同也位置相同的 A 的个数,同时也把所有数字出现的个数记录下来
- 遍历一遍数字出现的个数,去两个最小值,就是当前数字 B 出现的个数
class Solution {
public String getHint(String secret, String guess) {
int[] sArr = new int[10];
int[] gArr = new int[10];
int a = 0;
int b = 0;
for (int i = 0; i < secret.length(); i++) {
if (secret.charAt(i) == guess.charAt(i)) {
a++;
} else {
sArr[secret.charAt(i) - '0']++;
gArr[guess.charAt(i) - '0']++;
}
}
for (int i = 0; i < sArr.length; i++) {
b += Math.min(sArr[i], gArr[i]);
}
return a + "A" + b + "B";
}
}
执行耗时:5 ms,击败了89.58% 的Java用户
内存消耗:38.4 MB,击败了81.43% 的Java用户
