LeetCode 299 Bulls and Cows
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 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.
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的更多相关文章
- [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,数字对但位置不对给一 ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- 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 ...
随机推荐
- bzoj4621: Tc605
应要求写一下这个题的题解. 我的DP很奥(奇)妙(怪),不过跟标算还是殊途同归的(反正怎么做都行……) 先讲一下奥妙的性质吧. 首先,在最终序列中,每个数最多出现一段,并且,对于出现的数,每段数两两之 ...
- Android系统全貌 (转)
转自Gityuan的Android开篇,对自我学习作进一步整理. Android系统以Linux内核作为基底,上层采用Native层和Java层.系统分为内核空间和用户空间,并通过系统调用(Sysca ...
- Android源码——应用程序的消息处理机制
Android应用程序在启动每个线程时,都会创建一个消息队列.线程的生命周期分为创建消息队列和进入消息循环两个阶段. 消息循环分为:发送消息和处理消息. Android系统主要通过MessageQue ...
- 博文Contents<1--到200—>
====================-------------- 前言:博客中的随笔文章.并非都是笔者的原创文章.有些是听别人说的.有些是书上摘录的.有些是百度的.有些是别人博客的文章.有些是自己 ...
- Java学习笔记15
do-while循环时while循环的变体语法如下:do{ // 循环体 语句(组);}while(循环继续条件); 如果循环中的语句至少需要执行一次,那么建议使用do-while循环. for循环 ...
- [Unreal]学习笔记之材质说明
取消蓝图中的连接线:Alt+鼠标左键 在蓝图中,通过按住1,2,3,4加鼠标左键,可以快速生成1,2,3,4维的向量 材质和材质实例的区别:使用一个母材质,可以创建出多种场景中的材质实例:每次修改母材 ...
- 红米2A高配刷机记录
2014816 机型:红米2A高配版 设备型号:2014816 CPU:高通 线刷:fastboot平台 http://192.168.7.118/MesReports/Reports/Cutting ...
- 一起阅读《Java多线程编程核心技术》
目录 第一章 Java多线程技能 (待续...)
- DX 系列之 ComboBoxEdit
参考资料: ComboBoxEdit.Properties.Items=ComboBoxItemCollection
- github免密码设置
在创建好了github账号之后,我们可以新建自己的github项目.然而,我们在本地代码升级维护的过程中,涉及到git操作的时候并不是想每次都重新输入密码.这个时候我们需要使用ssh和私钥(公钥)来方 ...