class Solution {
public:
string To(int a){
if(a ==0) return "0";
string x="";
while(a>0){
x += char(a%10+'0');
a /= 10;
}
string y="";
for(int i = x.length()-1;i >= 0; i--){
y += x[i];
}
return y;
}
string getHint(string secret, string guess) {
int a = 0, b = 0;
int check[10],bak[10];
memset(bak,0,sizeof(bak));
memset(check,0,sizeof(check));
for(int i = 0;i < secret.length();i++){
if(secret[i] == guess[i]) a++;
else {
check[secret[i]-'0']++;
bak[guess[i]-'0']++;
}
}
for(int i = 0;i < 10;i++){
b += min(bak[i],check[i]);
}
return To(a)+"A"+To(b)+"B";
}
};
leetcode 299. Bulls and Cows
最新推荐文章于 2025-07-22 11:57:52 发布