leetcode笔记:Bulls and Cows
一. 题目描写叙述
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.
二. 题目分析
该题即是猜数字(又称 Bulls and Cows )是一种大概于20世纪中期兴起于英国的益智类小游戏。游戏规则抄送:http://baike.baidu.com/link?url=2OFEtJeMp3lxnGUt9tya4jVBwsUgCUM6C8F0zAdNTGtXS_KpjXa80lwIJpY_klUlL52JbO51fQYyqhs3fLMRLSsoSnGn7tvUOO6_iOxMTYbl7qEVhHdukYd1iTqqF-2uLDMXO9VuJFUTmmFN1qLFjVvQl6J6i7pr4Xd1oKJV-5_
在扫描过程前,创建一个数组用于记录0~9字符出现的次数。并在扫描时使用两种规则:
- 若字符在secret中出现一次则+1
- 若字符在guess中出现一次则-1
一个样例:当temp中’1’所相应的下标元素小于零时,此时说明guess此前出现过’1’的次数比secret多(或者说没有被secret出现’1’的次数抵消完),此时若secret出现’1’。则B++;反之,当temp中’1’所相应的下标元素大于零时。此时说明secret此前出现过’1’的次数比guess多(或者说没有被guess出现’1’的次数抵消完)。此时若guess出现’1’,则B++。
三. 演示样例代码
class Solution {
public:
string getHint(string secret, string guess) {
int temp[10] = {0}; // 用于存放0~9字符出现的次数
int SIZE = secret.size();
int A = 0, B = 0;
for (int i = 0; i < SIZE; ++i)
{
if (secret[i] == guess[i])
{
++A;
continue;
}
else // 若字符在secret中出现一次则+1,在guess中出现一次则-1
{
if (temp[secret[i] - '0'] < 0)
++B;
++temp[secret[i] - '0'];
if (temp[guess[i] - '0'] > 0)
++B;
--temp[guess[i] - '0'];
}
}
char result[10] = {0};
sprintf(result, "%dA%dB", A, B);
return result;
}
};
四. 小结
该题尽管比較简单,但还是挺有意思的,最后输出xAxB也改了一小段时间。
leetcode笔记:Bulls and Cows的更多相关文章
- 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]299. 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 字符串处理 统计
A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...
- [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/ 题 ...
随机推荐
- [转]PHP中替换换行符
FROM :http://www.cnblogs.com/siqi/archive/2012/10/12/2720713.html //php 有三种方法来解决 //1.使用str_replace 来 ...
- junit5了解一下
要求java8及以上版本 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage https://junit.org/junit5/docs/ ...
- ASP.NET MVC4 HtmlHelper扩展类,实现分页功能 @Html.ShowPageNavigate
本文主要做了一个HtmHelper类的分页扩展函数,方便在视图中调用,有需要的朋友可以参考一下,希望对大家有所帮助. 1.扩展HtmlHelper类方法ShowPageNavigate output. ...
- 「BZOJ」「3262」陌上花开
CDQ分治 WA :在solve时,对y.z排序以后,没有处理「y.z相同」的情况,也就是说可能(1,2,3)这个点被放到了(2,2,3)的后面,也就是统计答案在前,插入该点在后……也就没有统计到! ...
- Verilog 加法器和减法器(4)
类似于行波进位加法器,用串联的方法也能够实现多位二进制数的减法操作. 比如下图是4位二进制减法逻辑电路图. 8位二进制减法的verilog代码如下: module subn(x, y, d,cin) ...
- iOS 加密的3种方法
//需要导入 #import <CommonCrypto/CommonCryptor.h> ==============MD5加密============ NSString *str ...
- C# 网络编程之webBrowser获取网页url和下载网页中图片
该文章主要是通过C#网络编程的webBrowser获取网页中的url并简单的尝试瞎子啊网页中的图片,主要是为以后网络开发的基础学习.其中主要的通过应用程序结合网页知识.正则表达式实现浏览.获取url. ...
- 浅谈Kmeans聚类
http://www.cnblogs.com/easymind223/archive/2012/10/30/2747178.html 聚类分析是一种静态数据分析方法,常被用于机器学习,模式识别,数据挖 ...
- 10个在UNIX或Linux终端上快速工作的建议
你有没有惊讶地看到有人在Unix/ Linux中工作得非常快,噼里啪啦的敲键盘,快速的启动命令,飞快地执行命令? 在本文中,我共享了一些在Linux中快速.高效工作所遵循的Unix/ Linux命令实 ...
- Android OpenGL ES和OpenGL一起学(二)------理解Viewport(视口)和坐标系Android OpenGL ES篇(转帖)
来自:http://www.cnblogs.com/xiaobo68688/archive/2011/12/01/2269985.html 首先我们在屏幕中心显示一个矩形,效果如图: // 代 ...