Skip to content

Commit 454704a

Browse files
committed
commit solution 299
1 parent e6075c8 commit 454704a

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [299. Nim 游戏](https://leetcode-cn.com/problems/nim-game/description/)
2+
3+
### 题目描述
4+
5+
<p>你在和朋友一起玩 <a href="https://baike.baidu.com/item/%E7%8C%9C%E6%95%B0%E5%AD%97/83200?fromtitle=Bulls+and+Cows&amp;fromid=12003488&amp;fr=aladdin" target="_blank">猜数字(Bulls and Cows)</a>游戏,该游戏规则如下:</p>
6+
7+
<ol>
8+
<li>你写出一个秘密数字,并请朋友猜这个数字是多少。</li>
9+
<li>朋友每猜测一次,你就会给他一个提示,告诉他的猜测数字中有多少位属于数字和确切位置都猜对了(称为&ldquo;Bulls&rdquo;, 公牛),有多少位属于数字猜对了但是位置不对(称为&ldquo;Cows&rdquo;, 奶牛)。</li>
10+
<li>朋友根据提示继续猜,直到猜出秘密数字。</li>
11+
</ol>
12+
13+
<p>请写出一个根据秘密数字和朋友的猜测数返回提示的函数,返回字符串的格式为 <code>xAyB</code> ,<code>x</code> 和 <code>y</code> 都是数字,<code>A</code> 表示公牛,用&nbsp;<code>B</code>&nbsp;表示奶牛。</p>
14+
15+
<ul>
16+
<li><code>xA</code> 表示有 <code>x</code> 位数字出现在秘密数字中,且位置都与秘密数字一致。</li>
17+
<li><code>yB</code> 表示有 <code>y</code> 位数字出现在秘密数字中,但位置与秘密数字不一致。</li>
18+
</ul>
19+
20+
<p>请注意秘密数字和朋友的猜测数都可能含有重复数字,每位数字只能统计一次。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong>示例 1:</strong></p>
25+
26+
<pre><strong>输入:</strong> secret = &quot;1807&quot;, guess = &quot;7810&quot;
27+
<strong>输出:</strong> &quot;1A3B&quot;
28+
<strong>解释:</strong> <code>1</code>&nbsp;公牛和&nbsp;<code>3</code>&nbsp;奶牛。公牛是 <code>8</code>,奶牛是 <code>0</code>, <code>1</code>&nbsp;和 <code>7</code>。</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre><strong>输入:</strong> secret = &quot;1123&quot;, guess = &quot;0111&quot;
33+
<strong>输出:</strong> &quot;1A1B&quot;
34+
<strong>解释: </strong>朋友猜测数中的第一个 <code>1</code>&nbsp;是公牛,第二个或第三个 <code>1</code>&nbsp;可被视为奶牛。</pre>
35+
36+
<p>&nbsp;</p>
37+
38+
<p><strong>说明: </strong>你可以假设秘密数字和朋友的猜测数都只包含数字,并且它们的长度永远相等。</p>
39+
40+
### 解题思路
41+
42+
43+
44+
### 具体解法
45+
46+
47+
#### **Golang**
48+
```go
49+
func getHint(secret string, guess string) string {
50+
var countS, countG [10]int
51+
bulls, cows := 0, 0
52+
for i := range secret {
53+
ns := int(secret[i] - '0')
54+
ng := int(guess[i] - '0')
55+
if ng == ns {
56+
bulls++
57+
continue
58+
}
59+
60+
if countG[ns] > 0 {
61+
cows++
62+
countG[ns]--
63+
} else {
64+
countS[ns]++
65+
}
66+
67+
if countS[ng] > 0 {
68+
cows++
69+
countS[ng]--
70+
} else {
71+
countG[ng]++
72+
}
73+
}
74+
75+
return fmt.Sprintf("%dA%dB", bulls, cows)
76+
}
77+
```
78+
79+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
/*
8+
* @lc app=leetcode.cn id=299 lang=golang
9+
*
10+
* [299] 猜数字游戏
11+
*/
12+
13+
// @lc code=start
14+
func getHint(secret string, guess string) string {
15+
var countS, countG [10]int
16+
bulls, cows := 0, 0
17+
for i := range secret {
18+
ns := int(secret[i] - '0')
19+
ng := int(guess[i] - '0')
20+
if ng == ns {
21+
bulls++
22+
continue
23+
}
24+
25+
if countG[ns] > 0 {
26+
cows++
27+
countG[ns]--
28+
} else {
29+
countS[ns]++
30+
}
31+
32+
if countS[ng] > 0 {
33+
cows++
34+
countS[ng]--
35+
} else {
36+
countG[ng]++
37+
}
38+
}
39+
40+
return fmt.Sprintf("%dA%dB", bulls, cows)
41+
}
42+
43+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetHint(t *testing.T) {
8+
var secret string
9+
var guess string
10+
secret = "1123"
11+
guess = "0111"
12+
getHint(secret, guess)
13+
14+
secret = "011123112"
15+
guess = "012321544"
16+
getHint(secret, guess)
17+
}

0 commit comments

Comments
 (0)