package cn.edu.xidian.sselab.hashtable;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author zhiyong wang
 * title: Bull And Cows
 * content:
 *
ou 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.
 *
 */
public class BullAndCows {

//自己没有想出来,参考大牛的解题思路
    //这里默认secret 与 guess的长度是相等的
    //这个地方求cow的值是通过新建一个数组来保存0到9这十个数字,初始化值都为0,如果在secret中,他对应的值加1,如果在guess中,他对应的值减1
    //这样如果在secret的某位的值在数组中显示小于0,说明他以前在guess中出现过,通过guess也是这样
    public String getHint(String secret, String guess){
        int[] numbers = new int[10];
        int bull = 0;
        int cow = 0;
        if(secret == null || guess == null) return null;
        if(secret.length() == 0 || guess.length() == 0)
            return "0A0B";
        for(int i=0;i<secret.length();i++){
            if(secret.charAt(i) == guess.charAt(i))
                bull++;
            else{
                if(numbers[secret.charAt(i) - '0']++ < 0) cow++;
                if(numbers[guess.charAt(i) - '0']-- > 0) cow++;
            }
        }
        return bull + "A" + cow + "B";
    }
    
}

Bull And Cows的更多相关文章

  1. [LeetCode] Bulls and Cows 公母牛游戏

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

  2. LeetCode 299 Bulls and Cows

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

  3. [Leetcode] Bulls and Cows

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

  4. Bulls and Cows

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

  5. 299. Bulls and Cows

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

  6. Java [Leetcode 229]Bulls and Cows

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

  7. Poj OpenJudge 百练 2389 Bull Math

    1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...

  8. BZOJ1754: [Usaco2005 qua]Bull Math

    1754: [Usaco2005 qua]Bull Math Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 374  Solved: 227[Submit ...

  9. [LeetCode299]Bulls and Cows

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

随机推荐

  1. otg线是什么,otg线和数据线的区别介绍

    OTG是什么? OTG主要应用于各种不同的设备或移动设备间的联接,进行数据交换.USB技术的发展,使得PC和周边设备能够通过简单方式.适度的制造成本将各种数 据传输速度的设备连接在一起.上述我们的应用 ...

  2. Scrum 学习笔记

    Scrum 学习笔记 敏捷火了非常长一段时间了,可是一直没有机会实践,如今開始组队实践了,哈哈,先好好研习下规则~~ 什么是 scrum Scrum是一个敏捷开发框架,是一个增量的.迭代的开发过程.在 ...

  3. iOS--RunLoop原理介绍

    什么是RunLoop RunLoop从字面上看是运行循环的意思,这一点也不错,它确实就是一个循环的概念,或者准确的说是线程中的循环. 本文一开始就提到有些程序是一个圈,这个圈本质上就是这里的所谓的Ru ...

  4. SQL Server 2008 安装指南

    一.安装需求: 1.硬件需求条件 硬件 需求 处理器 最低:1.4 GHz(x64处理器)注意:Windows Server 2008 for Itanium-Based Systems 版本需要In ...

  5. 点击文字可以选中相应的checkbox

    <html><head><title>中国站长天空-网页特效-表单特效-点击文字选中的复选框</title><meta http-equiv=&q ...

  6. padding and margin.

    padding is the space between the content and the border, whereas margin is the space outside the bor ...

  7. poj1828

    poj1828 [问题的描述]是这样的:程序猿的近亲 猴子(......)最近在进行王位争夺站. 题中使用二维坐标轴上的点(x,y)来代表猴子所占有的位置, 每只猴子占有一个坐标点.并且一个坐标点上面 ...

  8. 灵活运用绑定变量---declare匿名块使用绑定变量

    declare        type cur01 is ref cursor;     v_cur cur01;        v_match123 varchar2(2000);        v ...

  9. 浅谈html5某些新元素的用途

    大家都知道html是一种前端网页语言,从出现到现在已经经历了很多的版本了,但是随着html的不断发展,现在的html5已经不再是单一的前端页面语言了,html,javascript,css不再单纯的只 ...

  10. javascript的选项卡

    主要用的索引值 首先 写三个按钮 <input type="button" > <input type="button" > <i ...