关闭

[leetcode]Bulls and Cows

标签: leetcode
231人阅读 评论(0) 收藏 举报
分类:

今天在一道Easy的题上花费了很多的时间,先上题:

Bulls and Cows
Total Accepted: 7492 Total Submissions: 30491 Difficulty: Easy
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.

案例给的两种情况其实并没有很好展现这个规则,以至于我在提交的过程中碰到了许多案例才逐渐懂,需要注意的是以下的几点:
  1. 一开始的理解是只要朋友猜中的数字都可以称作为cow,但实际上并不是这样的(这一点是我题目没有看清),如例子中的(1123,0111)这组数据,输出的是1A1B并不是1A2B,所以在程序实现过程中,要注意对用过数字的消除;
  2. 中间有一次WA在(1122,1222)这组数据中,我的输出是3A1B,但是期望输出的3A0B,这就说明了bulls的等级是要高于cow的,也就是说程序要先判断晚bull的个数然后再去求cow,而不能在一个循环中就直接完事。
  3. 因为我用的是数组方法(事后感觉略不好),所以期间对数组下标的错误使用也让我WA了好几次。

    附上代码:

public class Solution {
    public String getHint(String secret, String guess) {
            int bullNum = 0, cowNum = 0, i;
            int num, num2;
            int inSecret[] = new int[10];
            boolean isAppear[] = new boolean[10];
            for(i = 0; i < 10; i++){
                inSecret[i] = 0;
                isAppear[i] = false;
            }
            for(i = 0; i < secret.length(); i++){
                num = secret.charAt(i) - '0';
                inSecret[num]++;
                isAppear[num] = true;
            }
            for(i = 0; i < secret.length(); i++){
               num2 = guess.charAt(i) - '0';
               if(secret.charAt(i) == guess.charAt(i)){
                   bullNum ++;
                   inSecret[num2] --;
               }
            }
            for(i = 0; i < secret.length(); i++){
                if(secret.charAt(i) != guess.charAt(i)){
                    num2 = guess.charAt(i) - '0';
                     if(inSecret[num2] != 0 && isAppear[num2]){
                           cowNum ++;
                           inSecret[num2] --;
                      }
                }
            }

            String res = bullNum + "" + "A" + cowNum + "" + "B";
            return res;
    }
}
题目本身其实并不复杂,但是还是暴露了我一向读题不认真,考虑不全面的问题。

题目链接:(https://leetcode.com/problems/bulls-and-cows/)
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20918次
    • 积分:999
    • 等级:
    • 排名:千里之外
    • 原创:80篇
    • 转载:23篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论