Skip to content

Commit 8ae68c0

Browse files
committed
update: README.md
1 parent e6fd627 commit 8ae68c0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

leetcode/0299.Bulls-and-Cows/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,38 @@ Output: "1A0B"
7575
- 计算下标一致并且对应下标的元素一致的个数,即x
7676
- secret和guess分别去除x个公牛的元素,剩下secret和guess求共同的元素个数就是y
7777
- 把x, y转换成字符串,分别与"A"和"B"进行拼接返回结果
78+
79+
## 代码
80+
```go
81+
package leetcode
82+
83+
import "strconv"
84+
85+
func getHint(secret string, guess string) string {
86+
cntA, cntB := 0, 0
87+
mpS := make(map[byte]int)
88+
var strG []byte
89+
n := len(secret)
90+
var ans string
91+
for i := 0; i < n; i++ {
92+
if secret[i] == guess[i] {
93+
cntA++
94+
} else {
95+
mpS[secret[i]] += 1
96+
strG = append(strG, guess[i])
97+
}
98+
}
99+
for _, v := range strG {
100+
if _, ok := mpS[v]; ok {
101+
if mpS[v] > 1 {
102+
mpS[v] -= 1
103+
} else {
104+
delete(mpS, v)
105+
}
106+
cntB++
107+
}
108+
}
109+
ans += strconv.Itoa(cntA) + "A" + strconv.Itoa(cntB) + "B"
110+
return ans
111+
}
112+
```

0 commit comments

Comments
 (0)