[299] Bulls and Cows

1. 题目描述

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.

For example:

Secret number: “1807”
Friend’s guess: “7810”
Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
Write a function to return a hint according to the secret number and friend’s guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return “1A3B”.

Please note that both secret number and friend’s guess may contain duplicate digits, for example:

Secret number: “1123”
Friend’s guess: “0111”
In this case, the 1st 1 in friend’s guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return “1A1B”.
You may assume that the secret number and your friend’s guess only contain digits, and their lengths are always equal.

题目大意为,给定两个等长的字符串,字符串中只包含数字,求两个字符串中的bulls和cows。其中,bulls表示在字符串同一位置的数值相同的数字的个数。cows表示,guess中的数字在secret中出现了,但是处在不正确的位置上的数字的个数。

2. 解题思路

首先观察一下给的案例1,1807和7810,这个例子很普通,数字8位置和值都相等,8是一个bulls,其他三个数107都在secret和guess中出现了,位置不对,所以这三个数是3个cows。那么再看一下案例2,1123和0111,其中第二个1位置和值相同,它是一个bulls,那么有多少个cows呢,答案也是1,第三或第四位置的1和第一个位置的1形成一对cows,问题就出在这个cows是1不是2,也就是说我们需要避免掉对于已经是bulls的数值确定其是不是cows。
下面说说解题的想法,首先确定有多少bulls很简单,只需要按位比较,相等的就是bulls,那么如何确定有多少cows呢?首先我们可以做一个统计,就是secret中有多少数字可能成为cows,也就是说secret中除去bulls,还剩下多少数字,题目描述中说了这个数只包含数值,那么按照每一位来看的话,一位上的可能就是0-9,那么可以先遍历一次,统计一下还有多少个0-9的数字等待着成为cows并保存起来。之后再遍历一次,当出现不相等的时候,查看有没有可以匹配的cows存在,如果有计数加1并删除一个cows。如2112,0121,首先统计bulls和待匹配的cows,bulls有一个,第二个位置上的1,待匹配的cows,(0,0)(1,1)(2,2),之后再扫描一遍,如果相同位置元素值不相等,查看有没有待匹配的cows,首先是0,(0,0)并没有待匹配的cows,然后是第三个位置的2,待匹配的有两个(2,2),所以找到一个cow且(2,2)->(2,1),之后第四个位置的1,待匹配的cows(1,1),所以找到第二个cow且(1,1)->(1,0),最终结果为1A2B。

3. Code

import java.lang.StringBuffer;

public class Solution {
    public String getHint(String secret, String guess) {
        int [] all = new int[10];
        int bulls = 0, cows = 0;
        for(int i = 0; i < secret.length(); ++i)
        {
            // 如果当前位置值相等,bulls数量加1
            if(secret.charAt(i) == guess.charAt(i)){ 
                ++bulls;
            }else{
                // 将secret中含有的数字放入
                all[secret.charAt(i) - '0'] += 1;
            }
        }

        for(int j = 0; j < secret.length(); ++j)
        {
            if(secret.charAt(j) != guess.charAt(j))
            {
                int charValue = guess.charAt(j) - '0';
                // 还有待匹配的cows
                if(all[charValue] > 0){
                    ++cows;
                    all[charValue]-=1;
                }
            }
        }
        StringBuffer sb = new StringBuffer();
        // 拼接字符串
        sb.append(bulls).append('A')
        .append(cows).append('B');
        return sb.toString();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章