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. swift版 关于微信支付的那点事

    今天心情那真是想要强奸吉娃娃的冲动  说白了就是不想做和工作沾边的任何事  但是也不能闲着啊  时间那么贵 之前就想把微信支付做一下  主要就是怕自己忘记了 今天难得有时间  就来简单的记录一下  旨 ...

  2. 用NetStream的appendBytes播放FLV

    public class MiniStream extends Sprite { private var _buffer:ByteArray = new ByteArray(); private va ...

  3. Java之模板方法模式(Template Method)

    Java之模板方法模式(Template Method) 1. 概念:定义一个算法的骨架,而将一些实现步骤延迟到子类中. 把不变的行为搬到超类,去除子类中重复的代码来体现他的优势. 2. UML图: ...

  4. lufylegend库 LButton

    lufylegend库 LButton <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  5. line-height系列(二)——对行内元素(文字、图片、兄弟元素)、块级元素设置line-height后的表现

    >原创文章,转载请注明来源! 二.对行内元素(文字.图片.兄弟元素).块级元素设置line-height后的表现 对块级元素无效,对行内元素有效.可继承给行内元素. 文字的line-height ...

  6. 关于我的PP0.1聊天软件(客户端)

    登陆界面: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  7. Git学习之路(1)-Git简介

    ▓▓▓▓▓▓ 大致介绍 Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds ...

  8. 进阶之初探nodeJS

    一.前言 在"初探nodeJS"随笔中,我们对于node有了一个大致地了解,并在最后也通过一个示例,了解了如何快速地开启一个简单的服务器. 今儿,再次看了该篇随笔,发现该随笔理论知 ...

  9. SQL Server事务、隔离级别详解(二十九)

    前言 事务一直以来是我最薄弱的环节,也是我打算重新学习SQL Server的出发点,关于SQL Server中事务将分为几节来进行阐述,Always to review the basics. 事务简 ...

  10. js获取当前时间戳

    当使用js时我们可以使用timestamp = (new Date()).valueOf();直接获取当前时区时间点的时间戳.注意:js中时间戳的单位是毫秒,而php中则是秒