[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 ...
随机推荐
- 使用OWIN作为WebAPI的宿主
前言 好吧,也没什么好说的,就是个技术的总结,直接生成MVC的项目,感觉好重,虽然各种东西很全 ...也许我是处女座? - -, OWIN呃,这里我就不解释了,自己也是一知半解,可以参考 Open W ...
- 理解CSS前景色和透明度
前面的话 颜色的出现让网页不再只是黑白,运用好颜色设计,能让网页增色不少.一个网页给人们留下的第一印象实际上就是它的整体颜色.关于如何设置颜色,请移步CSS的6种颜色模式.实际上,颜色的应用主要分为前 ...
- 透过HT for Web 3D看动画Easing函数本质
http://www.hightopo.com/guide/guide/plugin/form/examples/example_easing.html 50年前的这个月诞生了BASIC这门计算机语言 ...
- 不到一百行实现一个小siri
想要容易理解核心的特征计算的话建议先去看看我之前的听歌识曲的文章,传送门:http://www.cnblogs.com/chuxiuhong/p/6063602.html 本文主要是实现了一个简单的命 ...
- 第三篇:Entity Framework CodeFirst & Model 映射 续篇 EntityFramework Power Tools 工具使用
上一篇 第二篇:Entity Framework CodeFirst & Model 映射 主要介绍以Fluent API来实作EntityFramework CodeFirst,得到了大家一 ...
- Gradle project sync failed
在Android Studio中运行APP时出现了以下错误: gradle project sync failed. please fix your project and try again 解决的 ...
- Qt信号与槽自动关联机制
参考链接1:http://blog.csdn.net/skyhawk452/article/details/6121407 参考链接2:http://blog.csdn.net/memory_exce ...
- HTTP各状态码解释
状态码 含义 100 客户端应当继续发送请求.这个临时响应是用来通知客户端它的部分请求已经被服务器接收,且仍未被拒绝.客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应.服务器必 ...
- Windows消息机制
Windows的消息系统是由3个部分组成的: · 消息队列.Windows能够为所有的应用程序维护一个消息队列.应用程序必须从消息队列中获取消息,然后分派给某个窗口.· 消息循环.通过这个循环机制应用 ...
- 超级小的web手势库AlloyFinger发布
简介 针对多点触控设备编程的Web手势组件,快速帮助你的web程序增加手势支持,也不用再担心click 300ms的延迟了.拥有两个版本,无依赖的独立版和react版本.除了Dom对象,也可监听Can ...