标签:== leetcode public guess tco 位置 get number dig
比较两个字符串,有多少相同位置的字符是相同的,相同的字符放在不同的位置
class Solution {
public:
int digit[10];
string getHint(string secret, string guess) {
int a = 0, b = 0;
for (int i = 0;i < secret.length();i++)
{
if(secret[i]==guess[i])
{
a++;
continue;
}
digit[secret[i] - ‘0‘]++;
}
for (int i = 0;i < guess.length();i++)
{
if(secret[i]==guess[i])
continue;
int number = guess[i] - ‘0‘;
if(digit[number]==0)
continue;
b++;
digit[number]--;
}
string str = "";
str += to_string(a);
str += ‘A‘;
str += to_string(b);
str += ‘B‘;
return str;
}
};
标签:== leetcode public guess tco 位置 get number dig
原文地址:https://www.cnblogs.com/dacc123/p/12979634.html