File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
leetcode/0299.Bulls-and-Cows Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff 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+ ```
You can’t perform that action at this time.
0 commit comments