[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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章