You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it. Each time your friend guesses a number, you give a hint. The hint tells your friend how many digits are in the correct positions (called "bulls") and how many digits are in the wrong positions (called "cows"). Your friend will use those hints to find out 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 01 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.

Solution:

http://www.bubuko.com/infodetail-1174728.html

https://leetcode.com/discuss/67031/one-pass-java-solution

 public class Solution {
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] nums = new int[10];
for(int i = 0; i < secret.length(); ++i) {
if(secret.charAt(i) == guess.charAt(i))
bulls++;
else {
if(nums[secret.charAt(i) - '0'] < 0)
cows++;
if(nums[guess.charAt(i) - '0'] > 0)
cows++;
nums[secret.charAt(i) - '0']++;
nums[guess.charAt(i) - '0']--;
}
}
return bulls + "A" + cows + "B";
}
}

[Leetcode] Bulls 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 Bulls and Cows (简单题)

    题意: 给出两个数字,输出(1)有多少位是相同的(2)有多少位不在正确的位置上. 思路: 扫一遍,统计相同的,并且将两串中不同的数的出现次数分别统计起来,取小者之和就是第2个答案了. class So ...

  3. 【一天一道LeetCode】#299. Bulls and Cows

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...

  4. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  5. 299. Bulls and Cows - LeetCode

    Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...

  6. LeetCode 299 Bulls and Cows

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

  7. Java [Leetcode 229]Bulls and Cows

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

  8. Bulls and Cows leetcode

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

  9. LeetCode(45)-Bulls and Cows

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

随机推荐

  1. [2016-10-24]jQuery学习回顾笔记1.0

    一.如何把 jQuery 添加到网页 <script> 标签应该位于页面的 <head> 部分. <head> <script src="jquer ...

  2. tab 切换写法

    <script>        var oUL = document.getElementById('aboutTab-ul');        var oLi = oUL.getElem ...

  3. 1.Two Sum(c++)(附6ms O(n) accepted 思路和代码)

    问题描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...

  4. python之路二十一

    URL        - 两个    Views        - 请求的其他信息        from django.core.handlers.wsgi import WSGIRequest   ...

  5. matlab绘图基础

    matlab绘制条形图并分组显示: a =[1 2 3] b =[4 5 6] >> d=[a;b] d = 1 2 3 4 5 6 >> bar(d,'group') 修改横 ...

  6. LocalDB 静默安装

    cmd命令:msiexec /i SqlLocalDB.msi /qn IACCEPTSQLLOCALDBLICENSETERMS=YES 注意:需要以管理员身份运行

  7. CC2540串口输出调试功能

    可以用printf()做串口打印输出 这个功能非常简单,首先在工程管理下的preprocessor把串口打开HAL_UART=TRUE. 然后看我的npi.c文件,多了什么自己琢磨,懒点的就直接复制吧 ...

  8. 我的CodeF水A题之路

    Codeforces Round #359 (Div. 2) A. Free Ice Cream 题目链接:http://www.codeforces.com/problemset/problem/6 ...

  9. Java页面中文编码要转换两次encodeURI

    1.js文件中使用encodeURI()方法. login_name = encodeURI(encodeURI(login_name)); 2.action中URLDecoder解码 loginNa ...

  10. IBM CLI 和 ODBC

    Installing and Configuring DB2 Clients Running CLI/ODBC Programs The DB2 Call Level Interface (CLI) ...