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

经典的小游戏,hashtable解决之,代码如下:

 class Solution {
public:
string getHint(string secret, string guess) {
unordered_map<char, int> hash;
int countA, countB;
countA = countB = ;
vector<bool> solved(secret.size(), false);
for(int i = ; i < secret.size(); ++i){
++hash[secret[i]];
}
for(int i = ; i < secret.size(); ++i){
if(secret[i] == guess[i]){
countA++;
solved[i] = true;
--hash[secret[i]];
}
}
for(int i = ; i < guess.size(); ++i){
if(!solved[i] && hash[guess[i]] > ){
countB++;
--hash[guess[i]];
}
}
return toString(countA) + "A" + toString(countB) + "B";
} string toString(int i)
{
stringstream ss;
ss << i;
string tmp;
ss >> tmp;
return tmp;
}
};

LeetCode OJ:Bulls and Cows (公牛与母牛)的更多相关文章

  1. [leetcode]299. Bulls and Cows公牛和母牛

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

  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. Java [Leetcode 229]Bulls and Cows

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

  4. LeetCode(45)-Bulls and Cows

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

  5. Leetcode 299 Bulls and Cows 字符串处理 统计

    A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...

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

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

  7. [Leetcode] Bulls and Cows

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

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

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

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

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

随机推荐

  1. manager

    S 识别  M 买账 A-安排 R-认同 T-提问识别上级的沟通特点,判断形势,识别沟通的时机摆正自己的角色位置,礼多人不怪,回应情绪做好沟通准备,有策略,安排合适时间听取反馈意见,认同并接纳指导提问 ...

  2. C++文件路径的写法

    转自:http://blog.csdn.net/chengonghao/article/details/51057780 文件路径的表示可以分为绝对路径和相对路径: c++中\\是一种转义字符,他表示 ...

  3. 写写Web API基础

    前两天突然心血来潮,对WEB Api 来了复习兴趣,虽然有很长时间没用了,现在有点迷湖.呀的!在VS2013的MVC项目(基于VS2012/MVC4的,如果是VS2013下的MVc5时,创建时勾上We ...

  4. hadoop namenode HA集群搭建

    hadoop集群搭建(namenode是单点的)  http://www.cnblogs.com/kisf/p/7456290.html HA集群需要zk, zk搭建:http://www.cnblo ...

  5. Spring Tomcat启动过程

    入口,配置再web.xml中, 初始化applicationContext 创建webApplicationContext,这里可加载web.xml文件中定义了contextClass 获取conte ...

  6. 【前端】CSS3的calc()使用

    calc()对大家来说,或许很陌生,不太会相信calc()是css中的部分.因为看其外表像个函数,既然是函数为何又出现在CSS中呢?这一点也让我百思不得其解,今天有一同事告诉我,说CSS3中有一个属性 ...

  7. GCC精彩之旅

    在为Linux开发应用程序时,绝大多数情况下使用的都是C语言,因此几乎每一位Linux程序员面临的首要问题都是如何灵活运用C编译器.目前Linux下最常用的C语言编译器是GCC(GNU Compile ...

  8. hadoop 3.1.1 安装

    以下是本次搭建说使用的服务器 服务器IP分配 IP 节点名称 说明 192.168.172.130 master 主服务器 192.168.172.131 slave1 从服务器1 192.168.1 ...

  9. docker shipyard 问题

    安装 docker  shipyard curl -s https://shipyard-project.com/deploy | bash -s docker machine 创建的虚拟机无法直接运 ...

  10. $.extendGit 丢弃所有本地修改的方法

    git checkout . #本地所有修改的.没有的提交的,都返回到原来的状态 git stash #把所有没有提交的修改暂存到stash里面.可用git stash pop回复. git rese ...