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");
}

發佈了74 篇原創文章 · 獲贊 24 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章