[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/)
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012505618/article/details/49905439

OpenCL编程的开发环境与API

OpenCL 是异构计算时代最为典型的编程语言和编程模型。本课程重点向 OpenCL 编程的新学者介绍如何搭建 OpenCL 开发环境和OpenCL Host代码的流程。
  • 2015年11月17日 16:02

[leetcode-299]Bulls and Cows(java)

问题描述:这里写链接内容问题分析:这道题看起来很唬人,其实就是遍历两个字符串,看在某个位置是否相等,如果相等,就A++;如果不相等,就把secret中的字符和guess中的字符再分别存起来;然后再遍历...
  • zdavb
  • zdavb
  • 2015-10-31 10:48:27
  • 1878

leetcode -- Bulls and Cows -- 难理解题意,要看

https://leetcode.com/problems/bulls-and-cows/这里要注意重复的问题。要首先计算bull,然后再计算cow,被match过得secret digit不能再被用...
  • xyqzki
  • xyqzki
  • 2015-12-08 23:04:58
  • 604

【Leetcode】299-bulls-and-cows【Java实现】【hash table】

最近有空就刷一下leetcode,299题自己的方法居然要50ms+,都不好意思贴出来。在discussion中看到了一个hash的方法。学习了一下,很巧妙,加油吧! stem: You are ...
  • ALiTuTu
  • ALiTuTu
  • 2015-11-05 17:48:22
  • 1130

LeetCode299——Bulls and Cows我的解法

You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
  • booirror
  • booirror
  • 2015-11-28 14:53:27
  • 2321

leetcode299. Bulls and Cows

You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
  • tinkle181129
  • tinkle181129
  • 2016-01-23 16:08:19
  • 143

【LeetCode-299】Bulls and Cows

问题描述:   Bulls and Cows这个问题从本质上来讲就是给你两个字符串,让你求出这两个字符串中字符相同且位置相同的字符个数以及字符相同且位置不同的字符个数。记两个字符串分别为secret和...
  • liujiayu1015
  • liujiayu1015
  • 2015-11-14 16:00:37
  • 272

Leetcode 299. Bulls and Cows (Python)

终于瞌睡了题目大意给定两个字符串 位置相同,字符相同,A加一 位置不同,存在相同字符,B加一 算法就是用hash表做代码class Solution(object): def getHint(...
  • wds2006sdo
  • wds2006sdo
  • 2016-07-19 03:52:50
  • 3118

leetcode 299 Bulls and Cows C++

也是参考了别人的做法。 两个大小为10的数组。 相同位置相同则直接将bull+1; 否则把数字存入数组。 然后把每个位置较小的数累加到cow里面即可。 string getHint(...
  • a2331046
  • a2331046
  • 2016-06-05 16:47:47
  • 348

LeetCode Bulls and Cows 数组

思路:关键是理解题意: A的意思是位置正确且数字正确的个数; B的意思是位置不正确但数字正确的个数。public class Solution { public String getHin...
  • yeruby
  • yeruby
  • 2015-11-09 10:41:23
  • 1246
收藏助手
不良信息举报
您举报文章:[leetcode]Bulls and Cows
举报原因:
原因补充:

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