leetcode 299. Bulls and Cows
2016-04-10 16:56
337 查看
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:
Hint:
and
(The bull is
the cows are
Write a function to return a hint according to the secret number and friend's guess, use
indicate the bulls and
return
Please note that both secret number and friend's guess may contain duplicate digits, for example:
In this case, the 1st
friend's guess is a bull, the 2nd or 3rd
a cow, and your function should return
You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
class Solution {
string my_itoa(int n)
{
if (n == 0)
return "0";
string re;
while (n != 0)
{
re.insert(re.begin(), n % 10 + '0');
n /= 10;
}
return re;
}
public:
string getHint(string secret, string guess) {
int bull = 0,cow=0;
map<char, int>cnt1, cnt2;
for (int i = 0; i < secret.length(); i++)
{
if (secret[i] == guess[i])
bull++;
else
{
cnt1[secret[i]]++;
cnt2[guess[i]]++;
}
}
for (map<char, int>::iterator it = cnt1.begin(); it != cnt1.end(); it++)
{
map<char, int>::iterator itt = cnt2.find(it->first);
if (itt != cnt2.end())
cow += itt->second < it->second ? itt->second : it->second;
}
string hint;
hint += my_itoa(bull) + 'A' + my_itoa(cow) + 'B';
return hint;
}
};
accept
其实这题给hint裁结果比较有意思
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:
1bull
and
3cows.
(The bull is
8,
the cows are
0,
1and
7.)
Write a function to return a hint according to the secret number and friend's guess, use
Ato
indicate the bulls and
Bto 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
1in
friend's guess is a bull, the 2nd or 3rd
1is
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.
class Solution {
string my_itoa(int n)
{
if (n == 0)
return "0";
string re;
while (n != 0)
{
re.insert(re.begin(), n % 10 + '0');
n /= 10;
}
return re;
}
public:
string getHint(string secret, string guess) {
int bull = 0,cow=0;
map<char, int>cnt1, cnt2;
for (int i = 0; i < secret.length(); i++)
{
if (secret[i] == guess[i])
bull++;
else
{
cnt1[secret[i]]++;
cnt2[guess[i]]++;
}
}
for (map<char, int>::iterator it = cnt1.begin(); it != cnt1.end(); it++)
{
map<char, int>::iterator itt = cnt2.find(it->first);
if (itt != cnt2.end())
cow += itt->second < it->second ? itt->second : it->second;
}
string hint;
hint += my_itoa(bull) + 'A' + my_itoa(cow) + 'B';
return hint;
}
};
accept
其实这题给hint裁结果比较有意思
相关文章推荐
- Linux IO 多路复用是什么意思?
- 数据库分区初识
- 20145231第六周学习笔记
- Tsinsen A1504. Book(王迪) 数论,贪心
- struts2继承StrutsTypeConverter实现自定义类型转换器
- 构建之法阅读笔记06
- IOS xib在tableview上的简单应用(通过xib自定义cell)
- 数据库多张表导出到excel
- C++封装篇(上)
- JAVA学习总结一(截止4月10日)
- ios开发第二次
- windows系统镜像下载地址
- 官方首发_ask2问答系统下载3.0下载
- 栈溢出笔记1.8 字符串问题
- GIT note
- 二维矩阵由左上角到右下角只能向右或向下走所有可能路径取值之和最大值
- WEB项目中使用UEditor(富文本编辑器)
- 11_输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- 20145218 《Java程序设计》第六周学习总结
- 网页为什么会乱码?