Leetcode - Bulls and Cows

Question

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.

简述一下题意:

我和朋友玩游戏,游戏规则是,我随机说出一串数字(以字符串形式给出)让朋友去猜,把我说的数字串和他猜的数字串中的的各个数字进行比较,如果相同位置上的数字相同,则是一个bull,如果不同位置上出现了相同的数字,则是一个cow,否则什么也不是,如果某个数字已经成为bull或者cow,则其不再重复用于比较。已知这两个数字串的长度相同,求bull和cow的个数,按指定格式返回结果。

Java Code

//版本一:使用两个HashMap集合记录每个数字及其出现的次数
public String getHint(String secret, String guess) {

    int bulls = 0;
    int cows = 0;
    int len = secret.length();
    HashMap<String, Integer> secMap = new HashMap<>();
    HashMap<String, Integer> gueMap = new HashMap<>();

    for(int i = 0; i < len; ++i) {
        String sec = secret.charAt(i) + "";
        String gue = guess.charAt(i) + "";
        if(sec.equals(gue)) {
            bulls++;//如果secret和guess的对应位置数字相同,累计bulls的数目,且不将其放入map中
        }else {
            //将secret的每个数字和对应的出现次数存入HashMap
            if(secMap.containsKey(sec))
                secMap.put(sec, secMap.get(sec) + 1);
            else
                secMap.put(sec, 1);
            //将guess的每个数字和对应的出现次数存入HashMap
            if(gueMap.containsKey(gue))
                gueMap.put(gue, gueMap.get(gue) + 1);
            else 
                gueMap.put(gue, 1);
        }
    }

    //map遍历方式一:先得到map的keySet集合,遍历key时通过key拿到对应的value
    Set<String> secSet = secMap.keySet();
    Iterator<String> it = secSet.iterator();
    int secValue;//secNum必定存在,可以用int类型变量接收
    Integer gueValue;//gueNum可能为空,只能用包装类(对象类型)接收
    while(it.hasNext()) {
        String secKey = it.next();
        if((gueValue = gueMap.get(secKey)) != null) {
            secValue = secMap.get(secKey);
            //如果secret和guess存在相同数字,取较少的出现次数累加到cows中
            cows += secValue < gueValue ? secValue : gueValue;
        }
    }

    //map遍历方式二:直接得到map的Map.Entry对象,遍历时可同时访问每一对<key, value>
    Set<Map.Entry<String, Integer>> entrySet = secMap.entrySet();
    Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();
    int secValue;
    Integer gueValue;
    while(it.hasNext()) {
        Map.Entry<String, Integer> iterator = it.next();
        String secKey = iterator.getKey();
        if((gueValue = gueMap.get(secKey)) != null) {
            secValue = iterator.getValue();
            cows += secValue < gueValue ? secValue : gueValue;
        }
    }


    return (bulls + "A" + cows + "B");
}

//版本二:使用两个数组记录每个数字及其出现的次数
public String getHint2(String secret, String guess) {

    int bulls = 0;
    int cows = 0;
    int[] secTable = new int[10];
    int[] gueTable = new int[10];

    char sec, gue;
    int len = secret.length();
    for(int i = 0; i < len; ++i) {
        if((sec = secret.charAt(i)) == (gue = guess.charAt(i)))
            bulls++;
        else {
            secTable[sec - 48]++;//数字0的ASCII码为48
            gueTable[gue - 48]++;
        }
    }

    for(int i = 0; i < 10; ++i)
        cows += secTable[i] < gueTable[i] ? secTable[i] : gueTable[i];

    return (bulls + "A" + cows + "B");
}

你可能感兴趣的:(LeetCode,COW,bull)