[leetcode]Bulls and Cows

原创 2015年11月18日 12:39:56

今天在一道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/)
版权声明:本文为博主原创文章,未经博主允许不得转载。

关于LeetCode中Bulls and Cows的理解

题目如下: You are playing the following Bulls and Cows game with your friend: You write down a nu...

LeetCode--299. Bulls and Cows

Problem:You are playing the following Bulls and Cows game with your friend: You write down a number ...

LeetCode 299 Bulls and Cows

题目描述You are playing the following Bulls and Cows game with your friend: You write down a number and ...

LeetCode笔记:299. Bulls and Cows

又一个猜数字的游戏

Bulls and Cows-LeetCode

姑且先记录一下程序调试成功的一些感想吧。 1. 对于java容器的掌握不够熟练,也是后来程序运行速度较慢的原因。 2. 容器中删减和插入了新的数据以后,原来的角标和检索的都发生了什么事情? 看看...

leetcode299. Bulls and Cows

299. Bulls and Cows 问题描述: You are playing the following Bulls and Cows game with your friend...

Leetcode:299. Bulls and Cows(JAVA)

【问题描述】 You are playing the following Bulls and Cows game with your friend: You write down a num...

leetcode--Bulls and Cows

Bulls and Cows

LeetCode(45)-Bulls and Cows

题目:You are playing the following Bulls and Cows game with your friend: You write down a number and a...

leetcode笔记:Bulls and Cows

该题即是猜数字(又称 Bulls and Cows )是一种大概于20世纪中期兴起于英国的益智类小游戏。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode]Bulls and Cows
举报原因:
原因补充:

(最多只允许输入30个字)