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 562b046

Browse files
committed
feat: add solutions to lc problem: No.2347
No.2347.Best Poker Hand
1 parent c2a2a53 commit 562b046

File tree

5 files changed

+176
-33
lines changed

5 files changed

+176
-33
lines changed

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

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,73 @@ func bestHand(ranks []int, suits []byte) string {
177177

178178
```ts
179179
function bestHand(ranks: number[], suits: string[]): string {
180-
let flush = true;
181-
for (let i = 1; i < 5 && flush; ++i) {
182-
flush = suits[i] == suits[i - 1];
183-
}
184-
if (flush) {
180+
if (suits.every(v => v === suits[0])) {
185181
return 'Flush';
186182
}
187-
const cnt = new Array(14).fill(0);
188-
let pair = false;
189-
for (const x of ranks) {
190-
if (++cnt[x] == 3) {
183+
const count = new Array(14).fill(0);
184+
let isPair = false;
185+
for (const v of ranks) {
186+
if (++count[v] === 3) {
191187
return 'Three of a Kind';
192188
}
193-
pair = pair || cnt[x] == 2;
189+
isPair = isPair || count[v] === 2;
190+
}
191+
if (isPair) {
192+
return 'Pair';
193+
}
194+
return 'High Card';
195+
}
196+
```
197+
198+
### **Rust**
199+
200+
```rust
201+
impl Solution {
202+
pub fn best_hand(ranks: Vec<i32>, suits: Vec<char>) -> String {
203+
if suits.iter().all(|v| *v == suits[0]) {
204+
return "Flush".to_string();
205+
}
206+
let mut count = [0; 14];
207+
let mut is_pair = false;
208+
for &v in ranks.iter() {
209+
let i = v as usize;
210+
count[i] += 1;
211+
if count[i] == 3 {
212+
return "Three of a Kind".to_string();
213+
}
214+
is_pair = is_pair || count[i] == 2;
215+
}
216+
(if is_pair { "Pair" } else { "High Card" }).to_string()
217+
}
218+
}
219+
```
220+
221+
### **C**
222+
223+
```c
224+
char *bestHand(int *ranks, int ranksSize, char *suits, int suitsSize) {
225+
bool isFlush = true;
226+
for (int i = 1; i < suitsSize; i++) {
227+
if (suits[0] != suits[i]) {
228+
isFlush = false;
229+
break;
230+
}
231+
}
232+
if (isFlush) {
233+
return "Flush";
234+
}
235+
int count[14] = {0};
236+
bool isPair = false;
237+
for (int i = 0; i < ranksSize; i++) {
238+
if (++count[ranks[i]] == 3) {
239+
return "Three of a Kind";
240+
}
241+
isPair = isPair || count[ranks[i]] == 2;
242+
}
243+
if (isPair) {
244+
return "Pair";
194245
}
195-
return pair?'Pair':'High Card';
246+
return "High Card";
196247
}
197248
```
198249

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

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,73 @@ func bestHand(ranks []int, suits []byte) string {
158158

159159
```ts
160160
function bestHand(ranks: number[], suits: string[]): string {
161-
let flush = true;
162-
for (let i = 1; i < 5 && flush; ++i) {
163-
flush = suits[i] == suits[i - 1];
164-
}
165-
if (flush) {
161+
if (suits.every(v => v === suits[0])) {
166162
return 'Flush';
167163
}
168-
const cnt = new Array(14).fill(0);
169-
let pair = false;
170-
for (const x of ranks) {
171-
if (++cnt[x] == 3) {
164+
const count = new Array(14).fill(0);
165+
let isPair = false;
166+
for (const v of ranks) {
167+
if (++count[v] === 3) {
172168
return 'Three of a Kind';
173169
}
174-
pair = pair || cnt[x] == 2;
170+
isPair = isPair || count[v] === 2;
171+
}
172+
if (isPair) {
173+
return 'Pair';
174+
}
175+
return 'High Card';
176+
}
177+
```
178+
179+
### **Rust**
180+
181+
```rust
182+
impl Solution {
183+
pub fn best_hand(ranks: Vec<i32>, suits: Vec<char>) -> String {
184+
if suits.iter().all(|v| *v == suits[0]) {
185+
return "Flush".to_string();
186+
}
187+
let mut count = [0; 14];
188+
let mut is_pair = false;
189+
for &v in ranks.iter() {
190+
let i = v as usize;
191+
count[i] += 1;
192+
if count[i] == 3 {
193+
return "Three of a Kind".to_string();
194+
}
195+
is_pair = is_pair || count[i] == 2;
196+
}
197+
(if is_pair { "Pair" } else { "High Card" }).to_string()
198+
}
199+
}
200+
```
201+
202+
### **C**
203+
204+
```c
205+
char *bestHand(int *ranks, int ranksSize, char *suits, int suitsSize) {
206+
bool isFlush = true;
207+
for (int i = 1; i < suitsSize; i++) {
208+
if (suits[0] != suits[i]) {
209+
isFlush = false;
210+
break;
211+
}
212+
}
213+
if (isFlush) {
214+
return "Flush";
215+
}
216+
int count[14] = {0};
217+
bool isPair = false;
218+
for (int i = 0; i < ranksSize; i++) {
219+
if (++count[ranks[i]] == 3) {
220+
return "Three of a Kind";
221+
}
222+
isPair = isPair || count[ranks[i]] == 2;
223+
}
224+
if (isPair) {
225+
return "Pair";
175226
}
176-
return pair?'Pair':'High Card';
227+
return "High Card";
177228
}
178229
```
179230
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
char *bestHand(int *ranks, int ranksSize, char *suits, int suitsSize) {
2+
bool isFlush = true;
3+
for (int i = 1; i < suitsSize; i++) {
4+
if (suits[0] != suits[i]) {
5+
isFlush = false;
6+
break;
7+
}
8+
}
9+
if (isFlush) {
10+
return "Flush";
11+
}
12+
int count[14] = {0};
13+
bool isPair = false;
14+
for (int i = 0; i < ranksSize; i++) {
15+
if (++count[ranks[i]] == 3) {
16+
return "Three of a Kind";
17+
}
18+
isPair = isPair || count[ranks[i]] == 2;
19+
}
20+
if (isPair) {
21+
return "Pair";
22+
}
23+
return "High Card";
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn best_hand(ranks: Vec<i32>, suits: Vec<char>) -> String {
3+
if suits.iter().all(|v| *v == suits[0]) {
4+
return "Flush".to_string();
5+
}
6+
let mut count = [0; 14];
7+
let mut is_pair = false;
8+
for &v in ranks.iter() {
9+
let i = v as usize;
10+
count[i] += 1;
11+
if count[i] == 3 {
12+
return "Three of a Kind".to_string();
13+
}
14+
is_pair = is_pair || count[i] == 2;
15+
}
16+
(if is_pair { "Pair" } else { "High Card" }).to_string()
17+
}
18+
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
function bestHand(ranks: number[], suits: string[]): string {
2-
let flush = true;
3-
for (let i = 1; i < 5 && flush; ++i) {
4-
flush = suits[i] == suits[i - 1];
5-
}
6-
if (flush) {
2+
if (suits.every(v => v === suits[0])) {
73
return 'Flush';
84
}
9-
const cnt = new Array(14).fill(0);
10-
let pair = false;
11-
for (const x of ranks) {
12-
if (++cnt[x]== 3) {
5+
const count = new Array(14).fill(0);
6+
let isPair = false;
7+
for (const v of ranks) {
8+
if (++count[v]=== 3) {
139
return 'Three of a Kind';
1410
}
15-
pair = pair || cnt[x] == 2;
11+
isPair = isPair || count[v] === 2;
12+
}
13+
if (isPair) {
14+
return 'Pair';
1615
}
17-
return pair ? 'Pair' : 'High Card';
16+
return 'High Card';
1817
}

0 commit comments

Comments
(0)

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