算法:公牛和母牛299. Bulls and Cows

Posted 架构师易筋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法:公牛和母牛299. Bulls and Cows相关的知识,希望对你有一定的参考价值。

299. 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.

1. 两个数组解法

语句Math.min(secretarr[i], guessarr[i])有三个选项:

  1. 两者都为零 ( secretarr[i]=guessarr[i]=0)。它的意思是
    数字i是公牛。所以它不可能是一头牛。
    或数字i未出现在猜测或秘密中。
  2. 其中之一为零。
    如果guessarr[i]=0这意味着我们没有猜到那个数字。所以无法计算。
    如果secretarr[i] =0这意味着我们没有秘密拥有那个数字(或算作公牛)。猜测是否包含数字i并不重要。
  3. 数字的计数i的猜测是小比秘密数字的计数。我们猜到了我们猜测中数字的较低数字i。秘密有更多的数字,i但猜测无法预测。
  4. 一个数字的计数i的猜测是更大的比秘密数字的计数。我们只i从秘密数组中添加一个数字计数。这意味着我们只能添加一些我们已经秘密拥有的数字。有可能猜测重复一个数字绰绰有余。足够等于秘密数字的频率数。
public class Solution {
    public String getHint(String secret, String guess) {
        int len = secret.length();
		int[] secretarr = new int[10];
		int[] guessarr = new int[10];
		int bull = 0, cow = 0;
		for (int i = 0; i < len; ++i) {
			if (secret.charAt(i) == guess.charAt(i)) {
				++bull;
			} else {
				++secretarr[secret.charAt(i) - '0'];
				++guessarr[guess.charAt(i) - '0'];
			}
		}
		for (int i = 0; i < 10; ++i) {
			cow += Math.min(secretarr[i], guessarr[i]);
		}
		return "" + bull + "A" + cow + "B";
    }
}

2. 一个数组解法

这个想法是迭代 insecret和 in的数字guess并立即计算所有多头。对于母牛,维护一个数组,该数组存储在secret和 中出现的数量计数guess。当secret已经看到任何一个数字时,增加奶牛,guest反之亦然。

public String getHint(String secret, String guess) {
    int bulls = 0;
    int cows = 0;
    int[] numbers = new int[10];
    for (int i = 0; i<secret.length(); i++) {
        if (secret.charAt(i) == guess.charAt(i)) bulls++;
        else {
            if (numbers[secret.charAt(i)-'0']++ < 0) cows++;
            if (numbers[guess.charAt(i)-'0']-- > 0) cows++;
        }
    }
    return bulls + "A" + cows + "B";
}

以上是关于算法:公牛和母牛299. Bulls and Cows的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 299. 猜数字游戏(Bulls and Cows)

299. Bulls and Cows

299. Bulls and Cows

LeetCode_299. Bulls and Cows

Leetcode 299:Bulls and Cows

leetcode [299]Bulls and Cows