Bulls and Cows leetcode
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 0
, 1
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的更多相关文章
- 299. Bulls and Cows - LeetCode
Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- [Leetcode] Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- Java [Leetcode 229]Bulls and Cows
题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...
- LeetCode(45)-Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- leetcode笔记:Bulls and Cows
一. 题目描写叙述 You are playing the following Bulls and Cows game with your friend: You write down a numbe ...
随机推荐
- --@angularJS--$http服务与后台数据交互
1.httpBasic.html: <!DOCTYPE HTML><html ng-app="app"><head> <title& ...
- js连接sqlserver进行查询
<script language="JavaScript"> // 创建数据库对象 var objdbConn = new ActiveXObject("AD ...
- Canvas translate- 平移
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- linux下的Source命令的基本功能
source命令用法:source FileName作用:在当前bash环境下读取并执行FileName中的命令.注:该命令通常用命令“.”来替代.如:source .bash_rc 与 . .bas ...
- js运算符单竖杠“|”的作用
在js整数操作的时候,相当于去除小数点,parseInt.在正数的时候相当于Math.floor(),负数的时候相当于Math.ceil() 注: 1. Math.ceil()用作向上取整. 2. M ...
- CodeForces 725A
A. Jumping Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- javascript中对数据文本格式化的思考
在实际应用场景中,我们常常需将一些数据输出成更加符合人类习惯阅读的格式. 保留小数点后面两位 在一些要求精度没有那么准确的场景下,我们可以直接通过Number.prototype.toFixed()来 ...
- MongoDB基础之八 备份与恢复
Mongodb导出与导入 1: 导入/导出可以操作的是本地的mongodb服务器,也可以是远程的.所以,都有如下通用选项:-h host 主机--port port 端口-u username 用户名 ...
- Yii框架中的form表单
<?php//引入命名空间use yii\helpers\Html;?><?php //表单:Html::beginForm(提交地址,提交方法,属性数组);?><?=H ...
- PHP标准库(SPL)- SplDoublyLinkedList类(双向链表)
class SplDoublyLinkedList implements Iterator, Traversable, Countable, ArrayAccess { const IT_MODE_L ...