Skip to content

Commit 3e9d114

Browse files
committed
feat: add solutions to lc problem: No.2347
No.2347.Best Poker Hand
1 parent ad335e2 commit 3e9d114

File tree

6 files changed

+153
-86
lines changed

6 files changed

+153
-86
lines changed

solution/2300-2399/2347.Best Poker Hand/README.md

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:计数**
65+
66+
我们可以用哈希表 $s$ 统计花色的种类,如果哈希表 $s$ 的大小为 $1$,说明五张牌的花色相同,返回 `"Flush"`
67+
68+
接下来,我们用哈希表或数组 $cnt$ 统计每张牌的数量:
69+
70+
- 如果有任意一张牌的数量等于 $3$,返回 `"Three of a Kind"`
71+
- 否则,如果有任意一张牌的数量等于 $2$,返回 `"Pair"`
72+
- 否则,返回 `"High Card"`
73+
74+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $ranks$ 的长度。
75+
6476
<!-- tabs:start -->
6577

6678
### **Python3**
@@ -94,19 +106,15 @@ class Solution {
94106
if (s.size() == 1) {
95107
return "Flush";
96108
}
97-
int[] cnt = new int[20];
98-
for (int v : ranks) {
99-
++cnt[v];
100-
if (cnt[v] >= 3) {
109+
int[] cnt = new int[14];
110+
boolean pair = false;
111+
for (int x : ranks) {
112+
if (++cnt[x] == 3) {
101113
return "Three of a Kind";
102114
}
115+
pair = pair || cnt[x] == 2;
103116
}
104-
for (int v : cnt) {
105-
if (v == 2) {
106-
return "Pair";
107-
}
108-
}
109-
return "High Card";
117+
return pair ? "Pair" : "High Card";
110118
}
111119
}
112120
```
@@ -118,13 +126,18 @@ class Solution {
118126
public:
119127
string bestHand(vector<int>& ranks, vector<char>& suits) {
120128
unordered_set<char> s(suits.begin(), suits.end());
121-
if (s.size() == 1) return "Flush";
122-
vector<int> cnt(20);
123-
for (int v : ranks)
124-
if (++cnt[v] >= 3) return "Three of a Kind";
125-
for (int v : cnt)
126-
if (v == 2) return "Pair";
127-
return "High Card";
129+
if (s.size() == 1) {
130+
return "Flush";
131+
}
132+
int cnt[14]{};
133+
bool pair = false;
134+
for (int& x : ranks) {
135+
if (++cnt[x] == 3) {
136+
return "Three of a Kind";
137+
}
138+
pair |= cnt[x] == 2;
139+
}
140+
return pair ? "Pair" : "High Card";
128141
}
129142
};
130143
```
@@ -133,24 +146,24 @@ public:
133146
134147
```go
135148
func bestHand(ranks []int, suits []byte) string {
136-
s := map[byte]bool{}
137-
for _, v := range suits {
138-
s[v] = true
149+
s := map[byte]struct{}{}
150+
for _, c := range suits {
151+
s[c] = struct{}{}
139152
}
140153
if len(s) == 1 {
141154
return "Flush"
142155
}
143-
cnt := make([]int, 20)
144-
for _, v := range ranks {
145-
cnt[v]++
146-
if cnt[v] >= 3 {
156+
cnt := [14]int{}
157+
pair := false
158+
for _, x := range ranks {
159+
cnt[x]++
160+
if cnt[x] == 3 {
147161
return "Three of a Kind"
148162
}
163+
pair = pair || cnt[x] == 2
149164
}
150-
for _, v := range cnt {
151-
if v == 2 {
152-
return "Pair"
153-
}
165+
if pair {
166+
return "Pair"
154167
}
155168
return "High Card"
156169
}
@@ -159,7 +172,24 @@ func bestHand(ranks []int, suits []byte) string {
159172
### **TypeScript**
160173

161174
```ts
162-
175+
function bestHand(ranks: number[], suits: string[]): string {
176+
const s = new Set<string>();
177+
for (const c of suits) {
178+
s.add(c);
179+
}
180+
if (s.size == 1) {
181+
return 'Flush';
182+
}
183+
const cnt = new Array(14).fill(0);
184+
let pair = false;
185+
for (const x of ranks) {
186+
if (++cnt[x] == 3) {
187+
return 'Three of a Kind';
188+
}
189+
pair = pair || cnt[x] == 2;
190+
}
191+
return pair ? 'Pair' : 'High Card';
192+
}
163193
```
164194

165195
### **...**

solution/2300-2399/2347.Best Poker Hand/README_EN.md

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,15 @@ class Solution {
8787
if (s.size() == 1) {
8888
return "Flush";
8989
}
90-
int[] cnt = new int[20];
91-
for (int v : ranks) {
92-
++cnt[v];
93-
if (cnt[v] >= 3) {
90+
int[] cnt = new int[14];
91+
boolean pair = false;
92+
for (int x : ranks) {
93+
if (++cnt[x] == 3) {
9494
return "Three of a Kind";
9595
}
96+
pair = pair || cnt[x] == 2;
9697
}
97-
for (int v : cnt) {
98-
if (v == 2) {
99-
return "Pair";
100-
}
101-
}
102-
return "High Card";
98+
return pair ? "Pair" : "High Card";
10399
}
104100
}
105101
```
@@ -111,13 +107,18 @@ class Solution {
111107
public:
112108
string bestHand(vector<int>& ranks, vector<char>& suits) {
113109
unordered_set<char> s(suits.begin(), suits.end());
114-
if (s.size() == 1) return "Flush";
115-
vector<int> cnt(20);
116-
for (int v : ranks)
117-
if (++cnt[v] >= 3) return "Three of a Kind";
118-
for (int v : cnt)
119-
if (v == 2) return "Pair";
120-
return "High Card";
110+
if (s.size() == 1) {
111+
return "Flush";
112+
}
113+
int cnt[14]{};
114+
bool pair = false;
115+
for (int& x : ranks) {
116+
if (++cnt[x] == 3) {
117+
return "Three of a Kind";
118+
}
119+
pair |= cnt[x] == 2;
120+
}
121+
return pair ? "Pair" : "High Card";
121122
}
122123
};
123124
```
@@ -126,24 +127,24 @@ public:
126127
127128
```go
128129
func bestHand(ranks []int, suits []byte) string {
129-
s := map[byte]bool{}
130-
for _, v := range suits {
131-
s[v] = true
130+
s := map[byte]struct{}{}
131+
for _, c := range suits {
132+
s[c] = struct{}{}
132133
}
133134
if len(s) == 1 {
134135
return "Flush"
135136
}
136-
cnt := make([]int, 20)
137-
for _, v := range ranks {
138-
cnt[v]++
139-
if cnt[v] >= 3 {
137+
cnt := [14]int{}
138+
pair := false
139+
for _, x := range ranks {
140+
cnt[x]++
141+
if cnt[x] == 3 {
140142
return "Three of a Kind"
141143
}
144+
pair = pair || cnt[x] == 2
142145
}
143-
for _, v := range cnt {
144-
if v == 2 {
145-
return "Pair"
146-
}
146+
if pair {
147+
return "Pair"
147148
}
148149
return "High Card"
149150
}
@@ -152,7 +153,24 @@ func bestHand(ranks []int, suits []byte) string {
152153
### **TypeScript**
153154

154155
```ts
155-
156+
function bestHand(ranks: number[], suits: string[]): string {
157+
const s = new Set<string>();
158+
for (const c of suits) {
159+
s.add(c);
160+
}
161+
if (s.size == 1) {
162+
return 'Flush';
163+
}
164+
const cnt = new Array(14).fill(0);
165+
let pair = false;
166+
for (const x of ranks) {
167+
if (++cnt[x] == 3) {
168+
return 'Three of a Kind';
169+
}
170+
pair = pair || cnt[x] == 2;
171+
}
172+
return pair ? 'Pair' : 'High Card';
173+
}
156174
```
157175

158176
### **...**

solution/2300-2399/2347.Best Poker Hand/Solution.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ class Solution {
22
public:
33
string bestHand(vector<int>& ranks, vector<char>& suits) {
44
unordered_set<char> s(suits.begin(), suits.end());
5-
if (s.size() == 1) return "Flush";
6-
vector<int> cnt(20);
7-
for (int v : ranks)
8-
if (++cnt[v] >= 3) return "Three of a Kind";
9-
for (int v : cnt)
10-
if (v == 2) return "Pair";
11-
return "High Card";
5+
if (s.size() == 1) {
6+
return "Flush";
7+
}
8+
int cnt[14]{};
9+
bool pair = false;
10+
for (int& x : ranks) {
11+
if (++cnt[x] == 3) {
12+
return "Three of a Kind";
13+
}
14+
pair |= cnt[x] == 2;
15+
}
16+
return pair ? "Pair" : "High Card";
1217
}
1318
};
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
func bestHand(ranks []int, suits []byte) string {
2-
s := map[byte]bool{}
3-
for _, v := range suits {
4-
s[v] = true
2+
s := map[byte]struct{}{}
3+
for _, c := range suits {
4+
s[c] = struct{}{}
55
}
66
if len(s) == 1 {
77
return "Flush"
88
}
9-
cnt := make([]int, 20)
10-
for _, v := range ranks {
11-
cnt[v]++
12-
if cnt[v] >= 3 {
9+
cnt := [14]int{}
10+
pair := false
11+
for _, x := range ranks {
12+
cnt[x]++
13+
if cnt[x] == 3 {
1314
return "Three of a Kind"
1415
}
16+
pair = pair || cnt[x] == 2
1517
}
16-
for _, v := range cnt {
17-
if v == 2 {
18-
return "Pair"
19-
}
18+
if pair {
19+
return "Pair"
2020
}
2121
return "High Card"
2222
}

solution/2300-2399/2347.Best Poker Hand/Solution.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ public String bestHand(int[] ranks, char[] suits) {
77
if (s.size() == 1) {
88
return "Flush";
99
}
10-
int[] cnt = new int[20];
11-
for (int v : ranks) {
12-
++cnt[v];
13-
if (cnt[v] >= 3) {
10+
int[] cnt = new int[14];
11+
boolean pair = false;
12+
for (int x : ranks) {
13+
if (++cnt[x] == 3) {
1414
return "Three of a Kind";
1515
}
16+
pair = pair || cnt[x] == 2;
1617
}
17-
for (int v : cnt) {
18-
if (v == 2) {
19-
return "Pair";
20-
}
21-
}
22-
return "High Card";
18+
return pair ? "Pair" : "High Card";
2319
}
2420
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function bestHand(ranks: number[], suits: string[]): string {
2+
const s = new Set<string>();
3+
for (const c of suits) {
4+
s.add(c);
5+
}
6+
if (s.size == 1) {
7+
return 'Flush';
8+
}
9+
const cnt = new Array(14).fill(0);
10+
let pair = false;
11+
for (const x of ranks) {
12+
if (++cnt[x] == 3) {
13+
return 'Three of a Kind';
14+
}
15+
pair = pair || cnt[x] == 2;
16+
}
17+
return pair ? 'Pair' : 'High Card';
18+
}

0 commit comments

Comments
 (0)