Bulls and Cows
2015-11-22 13:41
423 查看
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 {
public:
string getHint(string secret, string guess) {
int n=secret.size();
int bulls=0,cows=0;
vector<bool>flag(n,true);
vector<bool>flag2(n,true);
for(int i=0;i<n;i++){
if(secret[i]==guess[i]){
bulls++;
flag[i]=false;
flag2[i]=false;
}
}
for(int i=0;i<n;i++){
if(flag[i]==false) continue;
for(int j=0;j<n;j++){
if(flag2[j]==false) continue;
if(secret[i]==guess[j]){
cows++;
flag[i]=false;
flag2[j]=false;
break;
}
if(flag[i]==false) break;
}
}
return to_string(bulls)+"A"+to_string(cows)+"B";
}
};
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 {
public:
string getHint(string secret, string guess) {
int n=secret.size();
int bulls=0,cows=0;
vector<bool>flag(n,true);
vector<bool>flag2(n,true);
for(int i=0;i<n;i++){
if(secret[i]==guess[i]){
bulls++;
flag[i]=false;
flag2[i]=false;
}
}
for(int i=0;i<n;i++){
if(flag[i]==false) continue;
for(int j=0;j<n;j++){
if(flag2[j]==false) continue;
if(secret[i]==guess[j]){
cows++;
flag[i]=false;
flag2[j]=false;
break;
}
if(flag[i]==false) break;
}
}
return to_string(bulls)+"A"+to_string(cows)+"B";
}
};
class Solution { public: string getHint(string secret, string guess) { int bulls=0; int cows=0; vector<int>numbers(10,0); for(int i=0;i<secret.size();i++){ if(secret[i]==guess[i]) bulls++; else{ if(numbers[secret[i]-'0']++<0) cows++; if(numbers[guess[i]-'0']-->0) cows++; } } return to_string(bulls)+"A"+to_string(cows)+"B"; } };
相关文章推荐
- Leetcode Range Sum Query 2D - Immutable
- 双调旅行售货员问题
- 【C++】中序线索化二叉树及其遍历
- SVM多类分类方法
- 【BZOJ】2216: [Poi2011]Lightning Conductor
- C#使用ODAC向Oracle数据库插入海量实时数据
- iOS引入JavaScriptCore引擎框架(二)
- linux常用命令
- 注册asp.net 4.0 到iis
- 20135205信息安全系统设计基础第十一周学习总结
- 【BZOJ】2212: [Poi2011]Tree Rotations
- iOS 音频学习(二)
- JDBC--处理Blob
- 大数据时代——从谢顿计划说开去
- Android学习手记(6) TabActivity和TabHost
- swoole编译安装
- 【BZOJ】2172: Mario填格子
- coderforce 492B Vanya and Lanterns
- 在Ubuntu 14.04中安装最新版Eclipse
- Sql Group by 使用