@@ -177,22 +177,73 @@ func bestHand(ranks []int, suits []byte) string {
177
177
178
178
``` ts
179
179
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 ])) {
185
181
return ' Flush' ;
186
182
}
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 ) {
191
187
return ' Three of a Kind' ;
192
188
}
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";
194
245
}
195
- return pair ? ' Pair ' : ' High Card' ;
246
+ return " High Card" ;
196
247
}
197
248
```
198
249
0 commit comments