299. 猜数字游戏

猜数字游戏

解法一: 哈希

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func getHint(secret string, guess string) string {
bulls := 0
cows := 0
sCount := make([]int, 10)
gCount := make([]int, 10)
n := len(secret)
for i := 0; i < n; i++ {
if secret[i] == guess[i] {
bulls++
} else {
sCount[secret[i]-'0']++
gCount[guess[i]-'0']++
}
}
for i := 0; i < 10; i++ {
cows += min(sCount[i], gCount[i])
}
return strconv.Itoa(bulls) + "A" + strconv.Itoa(cows) + "B"
}
作者

wuhunyu

发布于

2024-03-10

更新于

2025-01-15

许可协议