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 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.

string getHint(string secret, string guess) {
int bull = ;
int cow = ;
if (secret.size() != guess.size() || secret.empty()) { return "0A0B"; }
vector<int> map(, );
for (int i = ; i < guess.length(); ++i)
{
if (secret[i] == guess[i])
bull++;
else
map[secret[i] - '']++;
}
for (int i = ; i < guess.length(); ++i)
{
if (secret[i] != guess[i] && map[guess[i] - ''] > ) {
cow++;
map[guess[i] - '']--;
}
}
return to_string(bull) + "A" + to_string(cow) + "B";
}

Bulls and Cows leetcode的更多相关文章

  1. 299. Bulls and Cows - LeetCode

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

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

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

  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. 【一天一道LeetCode】#299. Bulls and Cows

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

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

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

  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. LeetCode(45)-Bulls and Cows

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

  9. leetcode笔记:Bulls and Cows

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

随机推荐

  1. Xpath语法格式整理

    http://www.cnblogs.com/Loofah/archive/2012/05/10/2494036.html 经常在工作中会使用到XPath的相关知识,但每次总会在一些关键的地方不记得或 ...

  2. Docker,容器,虚拟机和红烧肉

    Docker火了,有多火你自己看看下面的统计数据就知道了 在发布4个月的时间里,下载量就超过50000次,github上收到超过4000个star,涌现了超过100个贡献者,并且有超过150个项目和超 ...

  3. zoj3823--构造

    题目大意: 在n*n(n<=512)的网格上,从边界某个点出发,经过每个点一次且回到边界上,构造出一种方案使拐弯的数量至少为n*(n-1)-1次. 构造方法:我们可以手算出n=2~6时的方案. ...

  4. HDU 1006 [Tick Tick]时钟问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1006 题目大意:钟表有时.分.秒3根指针.当任意两根指针间夹角大于等于n°时,就说他们是happy的, ...

  5. WinForm 制作一个简单的计算器

    namespace WindowsFormsApplication6 { public partial class Form1 : Form { //存储上次点击了什么按钮,0代表什么都没有点击,1代 ...

  6. C#npoi导出excel一些自己的看法

    之前转过一篇类似的文章,那个是用C#自带的excel类操作Excel的,还有一种在c#上操作excel的方法便是npoi了,npoi是poi的C#版本. npoi操作excel有两种形式,一种是hss ...

  7. jstl__报错

    1.缺少JAR:解决的办法就是手动将jstl.jar和 standard.jar这两个jar包加入到web项目的WEB-INF/lib目录中或者是把jstl.jar.standard.jar复制到to ...

  8. VS中生成debug和release的小问题

    vs里生成dll,需要在解决方案上右键"属性",然后"配置",点击右上角"配置管理器(0)",选择debug或release,方可生效.不然 ...

  9. TPS及计算方法

    个事务,TPS为6 / 60s = 0.10 TPS.同时我们会知道事务的响应时间(或节拍),以此例,60秒完成6个事务也同时代表每个事务的响应时间或节拍为10秒.   利特尔法则  (Little' ...

  10. array_count_values:返回数组中所有值出现的次数

    $arr1 = ['a','b','c','d','e','e','a','a']; $arr = array_count_values($arr1);   echo '<pre>'; p ...