Skip to content

Commit 9e59b27

Browse files
authored
feat: add solutions to lc problem: No.0299 (doocs#2427)
No.0299.Bulls and Cows
1 parent 6c933c5 commit 9e59b27

File tree

7 files changed

+185
-108
lines changed

7 files changed

+185
-108
lines changed

solution/0200-0299/0299.Bulls and Cows/README.md

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,33 @@
5858

5959
## 解法
6060

61-
### 方法一
61+
### 方法一:计数
62+
63+
我们创建两个计数器 $cnt1$ 和 $cnt2$,分别用来统计秘密数字和朋友猜测的数字中每个数字出现的次数。同时,我们创建一个变量 $x$ 来统计公牛的数量。
64+
65+
然后我们遍历秘密数字和朋友猜测的数字,如果当前数字相同,我们就将 $x$ 加一。否则,我们分别将秘密数字和朋友猜测的数字中的当前数字的计数加一。
66+
67+
最后,我们遍历 $cnt1$ 中的每个数字,取 $cnt1$ 和 $cnt2$ 中当前数字的计数的最小值,然后将这个最小值加到变量 $y$ 中。
68+
69+
最后,我们返回 $x$ 和 $y$ 的值。
70+
71+
时间复杂度 $O(n)$,其中 $n$ 是秘密数字和朋友猜测的数字的长度。空间复杂度 $O(|\Sigma|)$,其中 $|\Sigma|$ 是字符集的大小,本题中字符集为数字,所以 $|\Sigma| = 10$。
6272

6373
<!-- tabs:start -->
6474

6575
```python
6676
class Solution:
6777
def getHint(self, secret: str, guess: str) -> str:
68-
x = y = 0
69-
cnt1 = [0] * 10
70-
cnt2 = [0] * 10
71-
for i in range(len(secret)):
72-
if secret[i] == guess[i]:
78+
cnt1, cnt2 = Counter(), Counter()
79+
x = 0
80+
for a, b in zip(secret, guess):
81+
if a == b:
7382
x += 1
7483
else:
75-
cnt1[int(secret[i])] += 1
76-
cnt2[int(guess[i])] += 1
77-
78-
for i in range(10):
79-
y += min(cnt1[i], cnt2[i])
80-
return f'{x}A{y}B'
84+
cnt1[a] += 1
85+
cnt2[b] += 1
86+
y = sum(min(cnt1[c], cnt2[c]) for c in cnt1)
87+
return f"{x}A{y}B"
8188
```
8289

8390
```java
@@ -108,18 +115,20 @@ class Solution {
108115
public:
109116
string getHint(string secret, string guess) {
110117
int x = 0, y = 0;
111-
vector<int> cnt1(10);
112-
vector<int> cnt2(10);
118+
int cnt1[10]{};
119+
int cnt2[10]{};
113120
for (int i = 0; i < secret.size(); ++i) {
114121
int a = secret[i] - '0', b = guess[i] - '0';
115-
if (a == b)
122+
if (a == b) {
116123
++x;
117-
else {
124+
} else {
118125
++cnt1[a];
119126
++cnt2[b];
120127
}
121128
}
122-
for (int i = 0; i < 10; ++i) y += min(cnt1[i], cnt2[i]);
129+
for (int i = 0; i < 10; ++i) {
130+
y += min(cnt1[i], cnt2[i]);
131+
}
123132
return to_string(x) + "A" + to_string(y) + "B";
124133
}
125134
};
@@ -128,24 +137,46 @@ public:
128137
```go
129138
func getHint(secret string, guess string) string {
130139
x, y := 0, 0
131-
cnt1 := make([]int, 10)
132-
cnt2 := make([]int, 10)
133-
for i := 0; i < len(secret); i++ {
134-
a, b := secret[i]-'0', guess[i]-'0'
140+
cnt1 := [10]int{}
141+
cnt2 := [10]int{}
142+
for i, c := range secret {
143+
a, b := int(c-'0'), int(guess[i]-'0')
135144
if a == b {
136145
x++
137146
} else {
138147
cnt1[a]++
139148
cnt2[b]++
140149
}
141150
}
142-
for i := 0; i < 10; i++ {
143-
y += min(cnt1[i], cnt2[i])
151+
for i, c := range cnt1 {
152+
y += min(c, cnt2[i])
153+
144154
}
145155
return fmt.Sprintf("%dA%dB", x, y)
146156
}
147157
```
148158

159+
```ts
160+
function getHint(secret: string, guess: string): string {
161+
const cnt1: number[] = Array(10).fill(0);
162+
const cnt2: number[] = Array(10).fill(0);
163+
let x: number = 0;
164+
for (let i = 0; i < secret.length; ++i) {
165+
if (secret[i] === guess[i]) {
166+
++x;
167+
} else {
168+
++cnt1[+secret[i]];
169+
++cnt2[+guess[i]];
170+
}
171+
}
172+
let y: number = 0;
173+
for (let i = 0; i < 10; ++i) {
174+
y += Math.min(cnt1[i], cnt2[i]);
175+
}
176+
return `${x}A${y}B`;
177+
}
178+
```
179+
149180
```php
150181
class Solution {
151182
/**
@@ -154,23 +185,22 @@ class Solution {
154185
* @return String
155186
*/
156187
function getHint($secret, $guess) {
157-
$cntA = 0;
158-
$cntB = 0;
159-
$len = strlen($secret);
160-
for ($i = 0; $i < $len; $i++) {
161-
if ($secret[$i] == $guess[$i]) {
162-
$cntA++;
188+
$cnt1 = array_fill(0, 10, 0);
189+
$cnt2 = array_fill(0, 10, 0);
190+
$x = 0;
191+
for ($i = 0; $i < strlen($secret); ++$i) {
192+
if ($secret[$i] === $guess[$i]) {
193+
++$x;
163194
} else {
164-
$hashtable[$secret[$i]] += 1;
195+
++$cnt1[(int) $secret[$i]];
196+
++$cnt2[(int) $guess[$i]];
165197
}
166198
}
167-
for ($i = 0; $i < $len; $i++) {
168-
if ($secret[$i] != $guess[$i] && $hashtable[$guess[$i]] > 0) {
169-
$cntB++;
170-
$hashtable[$guess[$i]] -= 1;
171-
}
199+
$y = 0;
200+
for ($i = 0; $i < 10; ++$i) {
201+
$y += min($cnt1[$i], $cnt2[$i]);
172202
}
173-
return $cntA . 'A' . $cntB . 'B';
203+
return "{$x}A{$y}B";
174204
}
175205
}
176206
```

solution/0200-0299/0299.Bulls and Cows/README_EN.md

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,33 @@ Note that only one of the two unmatched 1s is counted as a cow since the non-bul
5353

5454
## Solutions
5555

56-
### Solution 1
56+
### Solution 1: Counting
57+
58+
We create two counters, $cnt1$ and $cnt2$, to count the occurrence of each digit in the secret number and the friend's guess respectively. At the same time, we create a variable $x$ to count the number of bulls.
59+
60+
Then we iterate through the secret number and the friend's guess. If the current digit is the same, we increment $x$ by one. Otherwise, we increment the count of the current digit in the secret number and the friend's guess respectively.
61+
62+
Finally, we iterate through each digit in $cnt1$, take the minimum count of the current digit in $cnt1$ and $cnt2$, and add this minimum value to the variable $y$.
63+
64+
In the end, we return the values of $x$ and $y$.
65+
66+
The time complexity is $O(n)$, where $n$ is the length of the secret number and the friend's guess. The space complexity is $O(|\Sigma|)$, where $|\Sigma|$ is the size of the character set. In this problem, the character set is digits, so $|\Sigma| = 10$.
5767

5868
<!-- tabs:start -->
5969

6070
```python
6171
class Solution:
6272
def getHint(self, secret: str, guess: str) -> str:
63-
x = y = 0
64-
cnt1 = [0] * 10
65-
cnt2 = [0] * 10
66-
for i in range(len(secret)):
67-
if secret[i] == guess[i]:
73+
cnt1, cnt2 = Counter(), Counter()
74+
x = 0
75+
for a, b in zip(secret, guess):
76+
if a == b:
6877
x += 1
6978
else:
70-
cnt1[int(secret[i])] += 1
71-
cnt2[int(guess[i])] += 1
72-
73-
for i in range(10):
74-
y += min(cnt1[i], cnt2[i])
75-
return f'{x}A{y}B'
79+
cnt1[a] += 1
80+
cnt2[b] += 1
81+
y = sum(min(cnt1[c], cnt2[c]) for c in cnt1)
82+
return f"{x}A{y}B"
7683
```
7784

7885
```java
@@ -103,18 +110,20 @@ class Solution {
103110
public:
104111
string getHint(string secret, string guess) {
105112
int x = 0, y = 0;
106-
vector<int> cnt1(10);
107-
vector<int> cnt2(10);
113+
int cnt1[10]{};
114+
int cnt2[10]{};
108115
for (int i = 0; i < secret.size(); ++i) {
109116
int a = secret[i] - '0', b = guess[i] - '0';
110-
if (a == b)
117+
if (a == b) {
111118
++x;
112-
else {
119+
} else {
113120
++cnt1[a];
114121
++cnt2[b];
115122
}
116123
}
117-
for (int i = 0; i < 10; ++i) y += min(cnt1[i], cnt2[i]);
124+
for (int i = 0; i < 10; ++i) {
125+
y += min(cnt1[i], cnt2[i]);
126+
}
118127
return to_string(x) + "A" + to_string(y) + "B";
119128
}
120129
};
@@ -123,24 +132,46 @@ public:
123132
```go
124133
func getHint(secret string, guess string) string {
125134
x, y := 0, 0
126-
cnt1 := make([]int, 10)
127-
cnt2 := make([]int, 10)
128-
for i := 0; i < len(secret); i++ {
129-
a, b := secret[i]-'0', guess[i]-'0'
135+
cnt1 := [10]int{}
136+
cnt2 := [10]int{}
137+
for i, c := range secret {
138+
a, b := int(c-'0'), int(guess[i]-'0')
130139
if a == b {
131140
x++
132141
} else {
133142
cnt1[a]++
134143
cnt2[b]++
135144
}
136145
}
137-
for i := 0; i < 10; i++ {
138-
y += min(cnt1[i], cnt2[i])
146+
for i, c := range cnt1 {
147+
y += min(c, cnt2[i])
148+
139149
}
140150
return fmt.Sprintf("%dA%dB", x, y)
141151
}
142152
```
143153

154+
```ts
155+
function getHint(secret: string, guess: string): string {
156+
const cnt1: number[] = Array(10).fill(0);
157+
const cnt2: number[] = Array(10).fill(0);
158+
let x: number = 0;
159+
for (let i = 0; i < secret.length; ++i) {
160+
if (secret[i] === guess[i]) {
161+
++x;
162+
} else {
163+
++cnt1[+secret[i]];
164+
++cnt2[+guess[i]];
165+
}
166+
}
167+
let y: number = 0;
168+
for (let i = 0; i < 10; ++i) {
169+
y += Math.min(cnt1[i], cnt2[i]);
170+
}
171+
return `${x}A${y}B`;
172+
}
173+
```
174+
144175
```php
145176
class Solution {
146177
/**
@@ -149,23 +180,22 @@ class Solution {
149180
* @return String
150181
*/
151182
function getHint($secret, $guess) {
152-
$cntA = 0;
153-
$cntB = 0;
154-
$len = strlen($secret);
155-
for ($i = 0; $i < $len; $i++) {
156-
if ($secret[$i] == $guess[$i]) {
157-
$cntA++;
183+
$cnt1 = array_fill(0, 10, 0);
184+
$cnt2 = array_fill(0, 10, 0);
185+
$x = 0;
186+
for ($i = 0; $i < strlen($secret); ++$i) {
187+
if ($secret[$i] === $guess[$i]) {
188+
++$x;
158189
} else {
159-
$hashtable[$secret[$i]] += 1;
190+
++$cnt1[(int) $secret[$i]];
191+
++$cnt2[(int) $guess[$i]];
160192
}
161193
}
162-
for ($i = 0; $i < $len; $i++) {
163-
if ($secret[$i] != $guess[$i] && $hashtable[$guess[$i]] > 0) {
164-
$cntB++;
165-
$hashtable[$guess[$i]] -= 1;
166-
}
194+
$y = 0;
195+
for ($i = 0; $i < 10; ++$i) {
196+
$y += min($cnt1[$i], $cnt2[$i]);
167197
}
168-
return $cntA . 'A' . $cntB . 'B';
198+
return "{$x}A{$y}B";
169199
}
170200
}
171201
```

solution/0200-0299/0299.Bulls and Cows/Solution.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ class Solution {
22
public:
33
string getHint(string secret, string guess) {
44
int x = 0, y = 0;
5-
vector<int> cnt1(10);
6-
vector<int> cnt2(10);
5+
int cnt1[10]{};
6+
int cnt2[10]{};
77
for (int i = 0; i < secret.size(); ++i) {
88
int a = secret[i] - '0', b = guess[i] - '0';
9-
if (a == b)
9+
if (a == b) {
1010
++x;
11-
else {
11+
} else {
1212
++cnt1[a];
1313
++cnt2[b];
1414
}
1515
}
16-
for (int i = 0; i < 10; ++i) y += min(cnt1[i], cnt2[i]);
16+
for (int i = 0; i < 10; ++i) {
17+
y += min(cnt1[i], cnt2[i]);
18+
}
1719
return to_string(x) + "A" + to_string(y) + "B";
1820
}
1921
};
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
func getHint(secret string, guess string) string {
22
x, y := 0, 0
3-
cnt1 := make([]int, 10)
4-
cnt2 := make([]int, 10)
5-
for i := 0; i < len(secret); i++ {
6-
a, b := secret[i]-'0', guess[i]-'0'
3+
cnt1 := [10]int{}
4+
cnt2 := [10]int{}
5+
for i, c := range secret {
6+
a, b := int(c-'0'), int(guess[i]-'0')
77
if a == b {
88
x++
99
} else {
1010
cnt1[a]++
1111
cnt2[b]++
1212
}
1313
}
14-
for i := 0; i < 10; i++ {
15-
y += min(cnt1[i], cnt2[i])
14+
for i, c := range cnt1 {
15+
y += min(c, cnt2[i])
16+
1617
}
1718
return fmt.Sprintf("%dA%dB", x, y)
1819
}

0 commit comments

Comments
 (0)