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 e8a48a7

Browse files
committed
"Bulls and Cows"
1 parent cfa9f08 commit e8a48a7

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
2525

2626
| | Problem | Solution |
2727
| --- | ------------------------------------------------------------ | ------------------ |
28+
| 299 | [Bulls and Cows] | [C](src/299.c) |
29+
| 298 | [Binary Tree Longest Consecutive Sequence]| |
2830
| 297 | [Serialize and Deserialize Binary Tree] | [C++](src/297.cpp) |
2931
| 296 | [Best Meeting Point]| |
3032
| 295 | [Find Median from Data Stream] | [C](src/295.c) |
@@ -310,6 +312,8 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
310312
[LeetCode algorithm problems]: https://leetcode.com/problemset/algorithms/
311313

312314

315+
[Bulls and Cows]: https://leetcode.com/problems/bulls-and-cows/
316+
[Binary Tree Longest Consecutive Sequence]: https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/
313317
[Serialize and Deserialize Binary Tree]: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
314318
[Best Meeting Point]: https://leetcode.com/problems/best-meeting-point/
315319
[Find Median from Data Stream]: https://leetcode.com/problems/find-median-from-data-stream/

‎src/299.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <assert.h>
5+
6+
#define min(a,b) (a)<(b)?(a):(b)
7+
char* getHint(char* secret, char* guess) {
8+
char hashs[10] = { 0 };
9+
char hashg[10] = { 0 };
10+
int len = strlen(secret);
11+
int bulls = 0, cows = 0;
12+
int i;
13+
for (i = 0; i < len; i++) {
14+
if (secret[i] == guess[i]) {
15+
bulls++;
16+
}
17+
else {
18+
hashs[secret[i] - '0']++;
19+
hashg[guess[i] - '0']++;
20+
}
21+
}
22+
for (i = 0; i < 10; i++) {
23+
cows += min(hashs[i], hashg[i]);
24+
}
25+
26+
char *hint = (char *)malloc(5);
27+
sprintf(hint, "%dA%dB", bulls, cows);
28+
hint[5] = '0円';
29+
return hint;
30+
}
31+
32+
int main() {
33+
assert(strcmp(getHint("1807", "7810"), "1A3B") == 0);
34+
assert(strcmp(getHint("1234", "0111"), "0A1B") == 0);
35+
assert(strcmp(getHint("1122", "2211"), "0A4B") == 0);
36+
37+
printf("all tests passed!\n");
38+
39+
return 0;
40+
}

0 commit comments

Comments
(0)

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