【一天一道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 ...
随机推荐
- hdu 5340 (manacher)
Sample Input 2 abc abaadada Sample Output Yes No 判断是否能成为3个非空回文子串 manacher算法求出个点回文长度,在找出第一个和最后一个保存下 ...
- hdu 5014(贪+位运算)
题意:给你n+1个数(0->n),让你为这n+1个数在0->n中分别找一个数与其异或,求最后的最大值 思路:假设一个数5 (二进制1 0 1),则找的另一个数在5的0位上最好是1 , 1位 ...
- POJ 3261 可重叠k次最长重复子串
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13127 Accepted: 5842 Ca ...
- ThreadLocal基本原理及运用
ThreadLocal提供本地线程变量.这个变量里面的值(通过get方法获取)是和其他线程分割开来的,变量的值只有当前线程能访问到,不像一般的类型比如Person,Student类型的变量,只要访问到 ...
- log4j不生成日志文件的问题
直接看我的注解吧 注意地址的斜杠,还有地址别写什么相对地址了,这包太老了,服务器update一下兼容问题就出来了. #第一个参数定义达到什么程度就输出 第二第三....第N 定义输出的类型 #debu ...
- angular+ionic前后端分离开发项目中的使用
Ionic基于AngularJS构建而成,所以学习一些AngularJS的知识很有必要.Ionic并没有独立开发一套完整的Web应用框架,而是对AngularJS进行了扩展,给它添加了大量界面组件和其 ...
- JAVA中接口的使用
抽象类是从多个类中抽象出来的模板,如果将这种抽象进行的更彻底,那么就是接口(interface)了.什么是接口,简单的讲,接口就是抽象类的进一步抽象,这种进一步的抽象只定义了一种规范,而不需要关心具体 ...
- JavaScript 对象JavaScript 对象
JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... 此外,JavaScript 允许自定义对象. 所有事物都是对象 JavaScript 提供多个内建对象,比如 String. ...
- Java对象的内存布局以及对象所需内存大小计算详解
1. 内存布局 在HotSpot虚拟机中,对象的内存布局可以分为三部分:对象头(Header). 实例数据(Instance Data)和对齐填充(Padding). 1) 对象头(Header): ...
- 万众瞩目之下,ANGULAR 2终于正式发布啦!
转载:https://angular.io/ 怀着期盼的心情,终于盼到了稳定版本,那么我就可以专心研究了,不再为不定期的修复烦恼咯. 今天,在 Google 总部一个特别的聚会上,我们发布了 Angu ...