Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 626d528

Browse files
committed
feat: add solutions to lc problem: No.0299
No.0299.Bulls and Cows
1 parent 3439bc1 commit 626d528

File tree

6 files changed

+214
-67
lines changed

6 files changed

+214
-67
lines changed

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

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
<p><strong>说明: </strong>你可以假设秘密数字和朋友的猜测数都只包含数字,并且它们的长度永远相等。</p>
4343

44-
4544
## 解法
4645

4746
<!-- 这里可写通用的实现逻辑 -->
@@ -55,19 +54,19 @@
5554
```python
5655
class Solution:
5756
def getHint(self, secret: str, guess: str) -> str:
58-
a_cnt = b_cnt = 0
59-
nums1 = dict()
60-
nums2 = dict()
57+
x = y = 0
58+
cnt1 = [0] *10
59+
cnt2 = [0] *10
6160
for i in range(len(secret)):
6261
if secret[i] == guess[i]:
63-
a_cnt += 1
62+
x += 1
6463
else:
65-
nums1[secret[i]] = nums1.get(secret[i], 0) + 1
66-
nums2[guess[i]] = nums2.get(guess[i], 0) + 1
67-
for i, v in nums1.items():
68-
ifi in nums2:
69-
b_cnt += min(v, nums2[i])
70-
return f'{a_cnt}A{b_cnt}B'
64+
cnt1[int(secret[i])] += 1
65+
cnt2[int(guess[i])] += 1
66+
67+
fori in range(10):
68+
y += min(cnt1[i], cnt2[i])
69+
return f'{x}A{y}B'
7170
```
7271

7372
### **Java**
@@ -77,25 +76,78 @@ class Solution:
7776
```java
7877
class Solution {
7978
public String getHint(String secret, String guess) {
80-
int aCnt = 0, bCnt = 0;
81-
Map<Character, Integer> nums1 = new HashMap<>();
82-
Map<Character, Integer> nums2 = new HashMap<>();
79+
int x = 0, y = 0;
80+
int[] cnt1 = new int[10];
81+
int[] cnt2 = new int[10];
8382
for (int i = 0; i < secret.length(); ++i) {
84-
if (secret.charAt(i) == guess.charAt(i)) {
85-
++aCnt;
83+
int a = secret.charAt(i) - '0', b = guess.charAt(i) - '0';
84+
if (a == b) {
85+
++x;
8686
} else {
87-
nums1.put(secret.charAt(i), nums1.getOrDefault(secret.charAt(i), 0) +1);
88-
nums2.put(guess.charAt(i), nums2.getOrDefault(guess.charAt(i), 0) +1);
87+
++cnt1[a];
88+
++cnt2[b];
8989
}
9090
}
91+
for (int i = 0; i < 10; ++i) {
92+
y += Math.min(cnt1[i], cnt2[i]);
93+
}
94+
return String.format("%dA%dB", x, y);
95+
}
96+
}
97+
```
9198

92-
for (Map.Entry<Character, Integer> entry : nums1.entrySet()) {
93-
if (nums2.containsKey(entry.getKey())) {
94-
bCnt += Math.min(entry.getValue(), nums2.get(entry.getKey()));
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
string getHint(string secret, string guess) {
105+
int x = 0, y = 0;
106+
vector<int> cnt1(10);
107+
vector<int> cnt2(10);
108+
for (int i = 0; i < secret.size(); ++i)
109+
{
110+
int a = secret[i] - '0', b = guess[i] - '0';
111+
if (a == b) ++x;
112+
else
113+
{
114+
++cnt1[a];
115+
++cnt2[b];
95116
}
96117
}
97-
return String.format("%dA%dB", aCnt, bCnt);
118+
for (int i = 0; i < 10; ++i) y += min(cnt1[i], cnt2[i]);
119+
return to_string(x) + "A" + to_string(y) + "B";
98120
}
121+
};
122+
```
123+
124+
### **Go**
125+
126+
```go
127+
func getHint(secret string, guess string) string {
128+
x, y := 0, 0
129+
cnt1 := make([]int, 10)
130+
cnt2 := make([]int, 10)
131+
for i := 0; i < len(secret); i++ {
132+
a, b := secret[i]-'0', guess[i]-'0'
133+
if a == b {
134+
x++
135+
} else {
136+
cnt1[a]++
137+
cnt2[b]++
138+
}
139+
}
140+
for i := 0; i < 10; i++ {
141+
y += min(cnt1[i], cnt2[i])
142+
}
143+
return fmt.Sprintf("%dA%dB", x, y)
144+
}
145+
146+
func min(a, b int) int {
147+
if a < b {
148+
return a
149+
}
150+
return b
99151
}
100152
```
101153

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

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ Note that only one of the two unmatched 1s is counted as a cow since the non-bul
6363
<li><code>secret</code> and <code>guess</code> consist of digits only.</li>
6464
</ul>
6565

66-
6766
## Solutions
6867

6968
<!-- tabs:start -->
@@ -73,45 +72,98 @@ Note that only one of the two unmatched 1s is counted as a cow since the non-bul
7372
```python
7473
class Solution:
7574
def getHint(self, secret: str, guess: str) -> str:
76-
a_cnt = b_cnt = 0
77-
nums1 = dict()
78-
nums2 = dict()
75+
x = y = 0
76+
cnt1 = [0] *10
77+
cnt2 = [0] *10
7978
for i in range(len(secret)):
8079
if secret[i] == guess[i]:
81-
a_cnt += 1
80+
x += 1
8281
else:
83-
nums1[secret[i]] = nums1.get(secret[i], 0) + 1
84-
nums2[guess[i]] = nums2.get(guess[i], 0) + 1
85-
for i, v in nums1.items():
86-
ifi in nums2:
87-
b_cnt += min(v, nums2[i])
88-
return f'{a_cnt}A{b_cnt}B'
82+
cnt1[int(secret[i])] += 1
83+
cnt2[int(guess[i])] += 1
84+
85+
fori in range(10):
86+
y += min(cnt1[i], cnt2[i])
87+
return f'{x}A{y}B'
8988
```
9089

9190
### **Java**
9291

9392
```java
9493
class Solution {
9594
public String getHint(String secret, String guess) {
96-
int aCnt = 0, bCnt = 0;
97-
Map<Character, Integer> nums1 = new HashMap<>();
98-
Map<Character, Integer> nums2 = new HashMap<>();
95+
int x = 0, y = 0;
96+
int[] cnt1 = new int[10];
97+
int[] cnt2 = new int[10];
9998
for (int i = 0; i < secret.length(); ++i) {
100-
if (secret.charAt(i) == guess.charAt(i)) {
101-
++aCnt;
99+
int a = secret.charAt(i) - '0', b = guess.charAt(i) - '0';
100+
if (a == b) {
101+
++x;
102102
} else {
103-
nums1.put(secret.charAt(i), nums1.getOrDefault(secret.charAt(i), 0) +1);
104-
nums2.put(guess.charAt(i), nums2.getOrDefault(guess.charAt(i), 0) +1);
103+
++cnt1[a];
104+
++cnt2[b];
105105
}
106106
}
107+
for (int i = 0; i < 10; ++i) {
108+
y += Math.min(cnt1[i], cnt2[i]);
109+
}
110+
return String.format("%dA%dB", x, y);
111+
}
112+
}
113+
```
107114

108-
for (Map.Entry<Character, Integer> entry : nums1.entrySet()) {
109-
if (nums2.containsKey(entry.getKey())) {
110-
bCnt += Math.min(entry.getValue(), nums2.get(entry.getKey()));
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
string getHint(string secret, string guess) {
121+
int x = 0, y = 0;
122+
vector<int> cnt1(10);
123+
vector<int> cnt2(10);
124+
for (int i = 0; i < secret.size(); ++i)
125+
{
126+
int a = secret[i] - '0', b = guess[i] - '0';
127+
if (a == b) ++x;
128+
else
129+
{
130+
++cnt1[a];
131+
++cnt2[b];
111132
}
112133
}
113-
return String.format("%dA%dB", aCnt, bCnt);
134+
for (int i = 0; i < 10; ++i) y += min(cnt1[i], cnt2[i]);
135+
return to_string(x) + "A" + to_string(y) + "B";
114136
}
137+
};
138+
```
139+
140+
### **Go**
141+
142+
```go
143+
func getHint(secret string, guess string) string {
144+
x, y := 0, 0
145+
cnt1 := make([]int, 10)
146+
cnt2 := make([]int, 10)
147+
for i := 0; i < len(secret); i++ {
148+
a, b := secret[i]-'0', guess[i]-'0'
149+
if a == b {
150+
x++
151+
} else {
152+
cnt1[a]++
153+
cnt2[b]++
154+
}
155+
}
156+
for i := 0; i < 10; i++ {
157+
y += min(cnt1[i], cnt2[i])
158+
}
159+
return fmt.Sprintf("%dA%dB", x, y)
160+
}
161+
162+
func min(a, b int) int {
163+
if a < b {
164+
return a
165+
}
166+
return b
115167
}
116168
```
117169

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
string getHint(string secret, string guess) {
4+
int x = 0, y = 0;
5+
vector<int> cnt1(10);
6+
vector<int> cnt2(10);
7+
for (int i = 0; i < secret.size(); ++i)
8+
{
9+
int a = secret[i] - '0', b = guess[i] - '0';
10+
if (a == b) ++x;
11+
else
12+
{
13+
++cnt1[a];
14+
++cnt2[b];
15+
}
16+
}
17+
for (int i = 0; i < 10; ++i) y += min(cnt1[i], cnt2[i]);
18+
return to_string(x) + "A" + to_string(y) + "B";
19+
}
20+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func getHint(secret string, guess string) string {
2+
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'
7+
if a == b {
8+
x++
9+
} else {
10+
cnt1[a]++
11+
cnt2[b]++
12+
}
13+
}
14+
for i := 0; i < 10; i++ {
15+
y += min(cnt1[i], cnt2[i])
16+
}
17+
return fmt.Sprintf("%dA%dB", x, y)
18+
}
19+
20+
func min(a, b int) int {
21+
if a < b {
22+
return a
23+
}
24+
return b
25+
}
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
class Solution {
22
public String getHint(String secret, String guess) {
3-
int aCnt = 0, bCnt = 0;
4-
Map<Character, Integer> nums1= new HashMap<>();
5-
Map<Character, Integer> nums2= new HashMap<>();
3+
int x = 0, y = 0;
4+
int[] cnt1= new int[10];
5+
int[] cnt2= new int[10];
66
for (int i = 0; i < secret.length(); ++i) {
7-
if (secret.charAt(i) == guess.charAt(i)) {
8-
++aCnt;
7+
int a = secret.charAt(i) - '0', b = guess.charAt(i) - '0';
8+
if (a == b) {
9+
++x;
910
} else {
10-
nums1.put(secret.charAt(i), nums1.getOrDefault(secret.charAt(i), 0) + 1);
11-
nums2.put(guess.charAt(i), nums2.getOrDefault(guess.charAt(i), 0) + 1);
11+
++cnt1[a];
12+
++cnt2[b];
1213
}
1314
}
14-
15-
for (Map.Entry<Character, Integer> entry : nums1.entrySet()) {
16-
if (nums2.containsKey(entry.getKey())) {
17-
bCnt += Math.min(entry.getValue(), nums2.get(entry.getKey()));
18-
}
15+
for (int i = 0; i < 10; ++i) {
16+
y += Math.min(cnt1[i], cnt2[i]);
1917
}
20-
return String.format("%dA%dB", aCnt, bCnt);
18+
return String.format("%dA%dB", x, y);
2119
}
2220
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution:
22
def getHint(self, secret: str, guess: str) -> str:
3-
a_cnt = b_cnt = 0
4-
nums1 = dict()
5-
nums2 = dict()
3+
x = y = 0
4+
cnt1 = [0] *10
5+
cnt2 = [0] *10
66
for i in range(len(secret)):
77
if secret[i] == guess[i]:
8-
a_cnt += 1
8+
x += 1
99
else:
10-
nums1[secret[i]] =nums1.get(secret[i], 0) + 1
11-
nums2[guess[i]] =nums2.get(guess[i], 0) + 1
12-
fori, vinnums1.items():
13-
ifi in nums2:
14-
b_cnt+= min(v, nums2[i])
15-
return f'{a_cnt}A{b_cnt}B'
10+
cnt1[int(secret[i])] += 1
11+
cnt2[int(guess[i])] += 1
12+
13+
fori in range(10):
14+
y+= min(cnt1[i], cnt2[i])
15+
return f'{x}A{y}B'

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /