Problem:

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 01 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.

Summary:

这道题题意略难懂。。搞了半天才明白。。

题目要求输出secret string和friend's guess两个字符串中相同位数和不同位数,其中:

相同位数bull:代表两个字符串中字符和下标都相同的位数

不同位数cow:代表两个字符串中字符相同但下标不同的位数

Solution:

Hash Table分别记录两个字符串中相应位数不同时字符出现个数。

 class Solution {
public:
string getHint(string secret, string guess) {
int len = secret.size();
int sec[] = {}, gue[] = {};
int bull = , cow = ; for (int i = ; i < len; i++) {
int s = secret[i] - '', g = guess[i] - '';
if (s == g) {
bull++;
}
else {
if (sec[g]) {
cow++;
sec[g]--;
}
else {
gue[g]++;
} if (gue[s]) {
cow++;
gue[s]--;
}
else {
sec[s]++;
}
}
} return (to_string(bull) + "A" + to_string(cow) + "B");
}
};

LeetCode 299 Bulls and Cows的更多相关文章

  1. [leetcode]299. Bulls and Cows公牛和母牛

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  2. Leetcode 299 Bulls and Cows 字符串处理 统计

    A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...

  3. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  4. 299. Bulls and Cows - LeetCode

    Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...

  5. 【一天一道LeetCode】#299. Bulls and Cows

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...

  6. 299. Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  7. 299 Bulls and Cows 猜数字游戏

    你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...

  8. Java [Leetcode 229]Bulls and Cows

    题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...

  9. LeetCode(45)-Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

随机推荐

  1. win7下安装mysql后修改密码

    mysql的安装教程网上很多,此处不过多介绍,个人觉得下面这篇教程是比较好的,一步到位.MySQL 5.7.9 ZIP 免安装版本配置过程_百度经验  http://jingyan.baidu.com ...

  2. OI省选算法汇总

    copy from hzwer @http://hzwer.com/1234.html 侵删 1.1 基本数据结构 1. 数组 2. 链表,双向链表 3. 队列,单调队列,双端队列 4. 栈,单调栈 ...

  3. 终端指令操作创建Django项目

    需求:通过Django创建一个用户表和权限表. 用户表包括:用户名,邮箱,密码,管理权限. 权限表包括:普通用户,管理用户,超级用户. 权限表和用户表有一对多的关系,即用户表中的每条数据对应权限表中的 ...

  4. 渗透测试-信息收集-c段收集

    平时做渗透测试我比较喜欢用lijiejie 写的 subDomainsBrute来爆破子域名 那么爆破完成后就想收集一下网站的c段信息 下面以平安为例 爆破得到子域名为 i.pingan.com.cn ...

  5. Jquery datatables 使用方法

    说明: 1.s开头的是字符串 2.b开头的是布尔值 3.i开头的是整型值 4.o开头的是Json对象 5.ao开头的是Json对象数组 6.aa开头的是二维数组 7.fn开头的是函数 服务器端返回的数 ...

  6. CentOS7清理yum缓存和释放内存方法

    清理yum缓存 清理yum缓存使用yum clean 命令,yum clean 的参数有headers, packages, metadata, dbcache, plugins, expire-ca ...

  7. OOP,WEB开发实用小技巧

    偶然读到一篇博客,记录一下心得.这种设计对于新手来说一般是想不到的,它充分的发挥了OOP语言的特性,让代码专用而清爽.这是不是重构的思想呢? 我们在写业务层的时候,有很多方法是重复功能的,我们就可以使 ...

  8. mybatis:choose when otherwise标签

    choose标签是按顺序判断其内部when标签中的test条件是否成立,如果有一个成立,则 choose 结束. 当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的 ...

  9. 第八章 交互技术,8.4 Weex 双11会场大规模应用的秒开实战和稳定性保障(作者:鬼道)

    8.4 Weex 双11会场大规模应用的秒开实战和稳定性保障 前言 Native 开发的诸多亮点中,流畅体验和系统调用是最多被提及的.流畅体验体现在页面滚动/动画的流畅性,背后是更好的内存管理和更接近 ...

  10. eclipse自动补全的设置

    eclipse自动补全的设置   如果你用过Visual Studio的自动补全功能后,再来用eclipse的自动补全功能,相信大家会有些许失望. 但是eclipse其实是非常强大的,eclipse的 ...