【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
来源: https://leetcode.com/problems/bulls-and-cows/
(二)解题
题目大意:给定两个string变量a和b,bulls表示a和b相同位上有相同数的个数,cows表示a和b不同位上有相同数的个数。注意计算过的不能再算。
解题思路:分为两类来计算,如1807和7810
(1)相同位上有相同数(8,8)
直接统计这样的位数即可
(2)相同位上有不同数(107和710)
先统计每个数出现
具体思路见代码注释:
class Solution {
public:
string getHint(string secret, string guess) {
int hash[10] = {0};//0~9在secret出现的次数
int len = secret.length();
int countA = 0 ,countB=0;
int *isFind = new int[len];//统计那些位上的数相同
memset(isFind,0,len*sizeof(int));
for(int i =0 ; i < len ;i++)
{
if(secret[i]==guess[i]){//相同位上有相同数
isFind[i] = 1;
countA++;
}
else hash[secret[i]-'0']++;//如果相同位上数不相等就记录下来
}
for(int i =0 ; i < len ;i++)
{
if(isFind[i]==0){//跳过相同位上有相同数
if(hash[guess[i]-'0']!=0)//如果出现过
{
hash[guess[i]-'0']--;//次数减1
countB++;
}
}
}
string ret;
stringstream ss;
ss<<countA;
ss<<'A';
ss<<countB;
ss<<'B';
ss>>ret;//按特定格式输出
return ret;
}
};
【一天一道LeetCode】#299. 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 ...
- [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】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 299. Bulls and Cows - LeetCode
Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...
- 299. Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- 299 Bulls and Cows 猜数字游戏
你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...
- 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 ...
随机推荐
- 51Nod 1331 狭窄的通道
有一个长为L的狭窄通道,我们假设这个通道在x轴上,其两个出口分别在x=0与x=L处.在这个通道里有N只狼,第i只狼有一个初始位置ai,它想到达位置bi(0<=i=L处空间足够大可以装下任意数量的 ...
- java continue与break区别
在循环体中跳出循环语句有continue与break语句 continue:跳出本次循环,包括本次循环continue后面的语句, break:跳出循环体,就是说一遇到break循环就结束. 代码: ...
- mysql免安装版下载及配置教程
第一步:下载 下载地址:http://dev.mysql.com/downloads/mysql/ 滚动到下方就能看到了,根据自己的需求下载: 我的电脑为64为的所以下载的为 Windows (x86 ...
- C语言第五次作业——循环结构
C语言程序设计第五次作业--循环结构(1) (一)改错题 输出华氏摄氏温度转换表:输入两个整数lower和upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是{lower,upper},每次增 ...
- 数据结构 栈&队列
2-4 依次在初始为空的队列中插入元素a,b,c,d以后,紧接着做了两次删除操作,此时的队头元素是( ) 删除,移动头指针: 增加,移动尾指针: 删除a,b ,队头c 2-3 在一个链队列中,fron ...
- VS生成项目时,有些文件无法复制到输出目录的解决办法
有时候,我们在生成项目时,发现有些文件如:.jpg的图片文件,无法复制到输出目录中,此时会非常纠结,反复的清理项目,重新生成,依旧不能解决此问题.后来我打开.csproj的项目工程文件时,经过对比发现 ...
- python3全栈开发-多进程的守护进程、进程同步、生产者消费者模式(重点)
一.守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemonic processes a ...
- easyui datagrid 动态表头2
前端 function goqry() { $("#form").form("submit", { url: "CJCK_bjcjfx_yfsl.as ...
- SpringMVC mock测试详解
@RunWith(SpringRunner.class) @SpringBootTest(classes = WebmanagerApplication.class) //配置事务的回滚,对数据库的增 ...
- Git 常用命令速查表