【LeetCode】299 Bulls and Cows (java实现)

版权声明:文章欢迎转载,但请注明出处。 https://blog.csdn.net/styshoo/article/details/51166829

原题:

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的个数,方法结构如下,数字都以字符串的形式表现,返回值也是,其中,A表示bulls,B表示cows。

public String getHint(String secret, String guess)

  这里需要注意的是,如果bulls中已经出现的数字,就不能再算到cows中了,这就是上面的用例中,为什么会返回1A1B了。

常规解法

  将secret和guess都解析成两个map,map的key就是位数,而value是位置上对应的数字。然后进行两轮轮训,第一轮找出bulls,然后从map中删除bulls;第二轮再找出cows,需要两次循环。
  这种解法很常规,也很容易想到,但代码很臃肿,也没有什么新意。

public class Solution {
    HashMap<Integer, Integer> getMap(String numString) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

        int size = numString.length();
        for (int i = 0; i < size; i++) {
            map.put(size -i, numString.charAt(i) - '0');
        }

        return map;
    }

    public String getHint(String secret, String guess) {
        int cntA = 0;
        int cntB = 0;

        HashMap<Integer, Integer> mapSecret = getMap(secret);
        HashMap<Integer, Integer> mapGuess = getMap(guess);
        int cntSecret = mapSecret.size();

        for (int i = 0; i < cntSecret; i++) {
            int bit = i + 1;
            if (!mapGuess.containsKey(bit)) {
                break;
            }

            if (mapSecret.get(bit) == mapGuess.get(bit)) {
                cntA++;
                mapSecret.remove(bit);
                mapGuess.remove(bit);
            }
        }

        for (int i = 0; i < cntSecret; i++) {
            int bit = i + 1;
            if (!mapSecret.containsKey(bit)) {
                continue;
            }

            for (Integer key : mapGuess.keySet()) {
                if (mapSecret.get(bit) == mapGuess.get(key)) {
                    cntB++;
                    mapGuess.remove(key);
                    break;
                }
            }
        }

        String ret = String.format("%dA%dB", cntA, cntB);
        return ret;

    }
}

  ps:我这里在代码中还验证了如果位数不相等的情况,但实际的用例中,好像并不用考虑这个。

更聪明的代码

public class Solution {
    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";
    }
}

  这个是discuss中的hot代码,真的是充满了智慧,尤其体现在处理cows的地方。创建了一个长度为10的int数组,因为每个位置的数字范围就是0——9,数组的index就对应0——9,而index对应的元素值表示secret中该index出现的次数。如果secret出现一次某个数字,该位置就自增1,如果此时该位置小于0,就说明这个数字在guess出现过,因此cows就自增1;guess的处理和secret类似,区别就是index对应元素需要自减。

没有更多推荐了,返回首页