[LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits are in the correct positions (called "bulls") and how many digits are in the wrong positions (called "cows"), your friend will use those hints to find out 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
.)
According to Wikipedia: "Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking mind or paper and pencil game for two or more players, predating the similar commercially marketed board game Mastermind. The numerical version of the game is usually played with 4 digits, but can also be played with 3 or any other number of digits."
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
.
You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
class Solution {
public:
string getHint(string secret, string guess) {
int m[] = {}, bulls = , cows = ;
for (int i = ; i < secret.size(); ++i) {
if (secret[i] == guess[i]) ++bulls;
else ++m[secret[i]];
}
for (int i = ; i < secret.size(); ++i) {
if (secret[i] != guess[i] && m[guess[i]]) {
++cows;
--m[guess[i]];
}
}
return to_string(bulls) + "A" + to_string(cows) + "B";
}
};
我们其实可以用一次循环就搞定的,在处理不是bulls的位置时,我们看如果secret当前位置数字的映射值小于0,则表示其在guess中出现过,cows自增1,然后映射值加1,如果guess当前位置的数字的映射值大于0,则表示其在secret中出现过,cows自增1,然后映射值减1,参见代码如下:
解法二:
class Solution {
public:
string getHint(string secret, string guess) {
int m[] = {}, bulls = , cows = ;
for (int i = ; i < secret.size(); ++i) {
if (secret[i] == guess[i]) ++bulls;
else {
if (m[secret[i]]++ < ) ++cows;
if (m[guess[i]]-- > ) ++ cows;
}
}
return to_string(bulls) + "A" + to_string(cows) + "B";
}
};
最后我们还可以稍作修改写的更简洁一些,a是bulls的值,b是bulls和cows之和,参见代码如下:
解法三:
class Solution {
public:
string getHint(string secret, string guess) {
int m[] = {}, a = , b = , i = ;
for (char s : secret) {
char g = guess[i++];
a += s == g;
b += (m[s]++ < ) + (m[g]-- > );
}
return to_string(a) + "A" + to_string(b - a) + "B";
}
};
参考资料:
https://leetcode.com/discuss/67031/one-pass-java-solution
https://leetcode.com/discuss/67125/short-c-o-n
https://leetcode.com/discuss/67012/c-one-pass-o-n-time-o-1-space
[LeetCode] Bulls and Cows 公母牛游戏的更多相关文章
- 299 Bulls and Cows 猜数字游戏
你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...
- [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 (简单题)
题意: 给出两个数字,输出(1)有多少位是相同的(2)有多少位不在正确的位置上. 思路: 扫一遍,统计相同的,并且将两串中不同的数的出现次数分别统计起来,取小者之和就是第2个答案了. class So ...
- [leetcode]299. Bulls and Cows公牛和母牛
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- LeetCode OJ:Bulls and Cows (公牛与母牛)
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- [Swift]LeetCode299. 猜数字游戏 | Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- leetcode笔记:Bulls and Cows
一. 题目描写叙述 You are playing the following Bulls and Cows game with your friend: You write down a numbe ...
随机推荐
- php后台增删改跳转
php登录页面: <h1>登录界面</h1> <form action="dengluchuli.php" method="post&quo ...
- js实现StringBuffer
实现 function StringBuffer() { this.__strings__ = []; }; StringBuffer.prototype.Append = function (str ...
- CSS布局 - 三栏布局
CSS布局技术可谓是前端技术中最基础的技术,就是因为基础,所以我认为要更加熟练,深入的去掌握,去梳理. 一. 传统 ---> 浮动实现的三栏布局 采用浮动实现的三栏布局有以下特点及注意事项: · ...
- java中异常抛出后代码还会继续执行吗
今天遇到一个问题,在下面的代码中,当抛出运行时异常后,后面的代码还会执行吗,是否需要在异常后面加上return语句呢? public void add(int index, E element){ i ...
- C#开机自动启动程序代码
新建一个winform拖一个checkbox进来.. 然后设置它的changed事件. 已经测试过,可以直接复制使用. private void checkBox1_CheckedChanged(ob ...
- asp.net MVC4——省市三级联动
controller: public ActionResult GetCity(string id) { AreaService _areaSvc = new AreaService(); List& ...
- EC笔记:第4部分:19、设计class犹如设计type
设计一个class,应该考虑以下问题: 新type的对象应该怎样创建和销毁? 构造函数 析构函数 内存分配 内存释放 对象的初始化和对象的赋值应该有什么样的差别? 拷贝构造函数 赋值运算符 新对象如果 ...
- 锐捷linux客户端常用命令(主要用来连接校园网或公司局域网)
锐捷访问校园网,.sh脚本文件rjsu*.sh-u 用户名-P 密码-S 参数1保存密码参数0不保存密码 其实: 直接使用md5认证方式输入用户名密码并且配置好ip之后,重新打开网卡即可有一定 ...
- 【开源】C#跨平台物联网通讯框架ServerSuperIO(SSIO)
[连载]<C#通讯(串口和网络)框架的设计与实现>-1.通讯框架介绍 [连载]<C#通讯(串口和网络)框架的设计与实现>-2.框架的总体设计 目 录 C#跨平台物联 ...
- FreeMarker的基础语法
FreeMarker语言 FreeMarker语言概述 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写. FreeMarker被设计用来生成HTML Web ...