LeetCode 299. 猜数字游戏C#
原题链接
简单
作者:
hpstory
,
2022-07-06 21:54:07
,
所有人可见
,
阅读 91
C# 代码
public class Solution {
public string GetHint(string secret, string guess) {
int[] count = new int[10];
int b = 0, total = 0;
foreach (char ch in secret){
count[ch - '0']++;
}
foreach (char ch in guess){
if (count[ch - '0'] > 0){
total++;
count[ch - '0']--;
}
// count[ch - '0'] == 0表示secret里面的字符已经用光了,或者没这个字符
}
for (int i = 0; i < secret.Length; i++){
if (secret[i] == guess[i]) b++;
}
return $"{b}A{total - b}B";
}
}