题目:

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.

代码:

class Solution {
public:
string getHint(string secret, string guess) {
int aCount = ;
int bCount = ;
vector<int> secretVec(,);
vector<int> guessVec(,);
if(secret.empty())
return "0A0B";
for(int i = ; i < secret.size(); ++i)
{
char c1 = secret[i];
char c2 = guess[i];
if(c1 == c2)
{
++aCount;
}
else
{
++secretVec[c1-''];
++guessVec[c2-''];
}
}
for(int i = ; i < secretVec.size(); ++i)
{
bCount += min(secretVec[i],guessVec[i]);
}
return to_string(aCount) + "A" + to_string(bCount) + "B";
}
};

[LeetCode299]Bulls and Cows的更多相关文章

  1. [Swift]LeetCode299. 猜数字游戏 | Bulls and Cows

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

  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 299 Bulls and Cows

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

  4. [Leetcode] Bulls and Cows

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

  5. Bulls and Cows

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

  6. 299. Bulls and Cows

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

  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】#299. Bulls and Cows

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

随机推荐

  1. 【iOS开发-76】Private Contacts案例:导航控制器使用、数据传递、第三方类库使用、tableViewCell的加入删除、数据存储等

    (1)效果 (2)源码与第三方类库下载 http://download.csdn.net/detail/wsb200514/8155979 (3)总结 --导航控制器,能够直接用代码的push和pop ...

  2. Android在子线程中更新UI(一)

    MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...

  3. unity3D游戏开发十八之NGUI动画

    我们先来看下帧动画,顾名思义,就是一帧帧的图片组成的动画,我们须要用到UISprite Animation组件,它的属性例如以下: Framerate:播放速率,也就是每秒钟播放的帧数 Name Pr ...

  4. yum 安装软件时报错

    报错信息 Another app is currently holding the yum lock; waiting for it to exit 处理方法 rm -rf /var/run/yum. ...

  5. cocos2d-x 2.2.3 创建项目的方法

    直接复制粘贴到txt文本,然后修改后缀为.bat,然后将bat文件放到tools\project-creator的目录下即可. :project_input @echo 请输入项目名称,按回车,例:H ...

  6. JavaScript的作用域和变量对象

    变量对象 先来说说什么是变量对象.变量对象中又存储了什么东西吧. JavaScript中的运行环境包含全局运行环境和函数运行环境这两种,每进入到一个运行环境都会创建一个变量对象,这个对象中记录了在当前 ...

  7. sscanf,sscanf_s及其相关使用方法

    #include<stdio.h> 定义函数 int sscanf (const char *str,const char * format,........); 函数说明   sscan ...

  8. Python学习入门基础教程(learning Python)--3.2 if-else分支语句

    if-else分支语句结构的特点是当conditon条件满足时,执行if下的语句块,当condition条件不满足时执行else下的语句块,也就是说根据条件来控制让某些语句执行,某些语句不被执行. i ...

  9. Codeforces Round#309 C Kyoya and Colored Balls

    给定一个k表示颜色的种类从1到k 然后接下来k行, 每行一个数字, 代表该颜色的球有多少个 这些球都放在一个包中,然后依次拿出.  要求颜色i的最后一个球, 必须要排在颜色i+1的最后一个球前面,   ...

  10. mysql重装后出现乱码解决办法

    查看当前连接系统参数:SHOW VARIABLES LIKE '%char%'; mysql> show variables like 'char%'; +------------------- ...