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. HDU 1025 Constructing Roads In JGShining&#39;s Kingdom (DP)

    Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which ...

  2. 利用Java API通过路径过滤上传多文件至HDFS

    在本地文件上传至HDFS过程中,很多情况下一个目录包含很多个文件,而我们需要对这些文件进行筛选,选出符合我们要求的文件,上传至HDFS.这时就需要我们用到文件模式. 在项目开始前,我们先掌握文件模式 ...

  3. java Map实现的cache manager

    一个模仿memcached的JAVA虚拟缓存工具,可以缓存java对象 import java.io.ByteArrayInputStream; import java.io.ByteArrayOut ...

  4. 上传文件时 ContentType 浏览器差异

    上传图片时,ie会把 jpg.jpeg翻译成image/pjpeg,png翻译成image/x-png . 火狐.chrome则很标准:jpg.jpeg翻译成image/jpeg,png翻译成imag ...

  5. Java基础知识强化之集合框架笔记29:使用LinkedList实现栈数据结构的集合代码(面试题)

    1. 请用LinkedList模拟栈数据结构的集合,并测试:  题目的意思是:     你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟,使用LinkedList功能方法封装成 ...

  6. codevs 访问艺术馆

    /* codevs 1163 访问艺术馆 红果果的树形dp*/ #include<iostream> #include<cstdio> #include<cstring& ...

  7. Weex 初始

    1.一旦数据和模板绑定,数据的变化会立即体现在前台的变化 <template> <container> <text style="font-size: {{si ...

  8. EF Lambda 多表查询

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mv ...

  9. Android开发---支付宝功能接口(支付功能)(转载!)

    最近在做一个关于购物商城的项目,项目里面付款这块我选的是调用支付宝的接口,因为用的人比较多. 在网上搜索了以下,有很多这方面的教程,但大部分教程过于陈旧,而且描述的过于简单.而且支付宝提供的接口一直在 ...

  10. VS2015升级Update2之后Cordova程序提示:此应用程序无法在此电脑上运行

    VS2015在升级到Update2之后,有可能出现如下异常,在运行Cordova项目时提示: 查看输出面板会有乱码错误信息: 出现此问题的原因是在于npm程序损坏了.vs调用的npm程序并不是在nod ...