输入:ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
输出:"Three of a Kind"
解释:第一、二和四张牌组成三张相同大小的扑克牌,所以得到 "Three of a Kind" 。
注意我们也可以得到 "Pair" ,但是 "Three of a Kind" 是更好的手牌类型。
有其他的 3 张牌也可以组成 "Three of a Kind" 手牌类型。
示例 3:
输入:ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
输出:"Pair"
解释:第一和第二张牌大小相同,所以得到 "Pair" 。
我们无法得到 "Flush" 或者 "Three of a Kind" 。
classSolution:defbestHand(self,ranks:List[int],suits:List[str])->str:# if len(set(suits)) == 1:ifall(a==bfora,binpairwise(suits)):return'Flush'cnt=Counter(ranks)ifany(v>=3forvincnt.values()):return'Three of a Kind'ifany(v==2forvincnt.values()):return'Pair'return'High Card'
1 2 3 4 5 6 7 8 91011121314151617181920
classSolution{publicStringbestHand(int[]ranks,char[]suits){booleanflush=true;for(inti=1;i<5&&flush;++i){flush=suits[i]==suits[i-1];}if(flush){return"Flush";}int[]cnt=newint[14];booleanpair=false;for(intx:ranks){if(++cnt[x]==3){return"Three of a Kind";}pair=pair||cnt[x]==2;}returnpair?"Pair":"High Card";}}
1 2 3 4 5 6 7 8 9101112131415161718192021
classSolution{public:stringbestHand(vector<int>&ranks,vector<char>&suits){boolflush=true;for(inti=1;i<5&&flush;++i){flush=suits[i]==suits[i-1];}if(flush){return"Flush";}intcnt[14]{};boolpair=false;for(int&x:ranks){if(++cnt[x]==3){return"Three of a Kind";}pair|=cnt[x]==2;}returnpair?"Pair":"High Card";}};
1 2 3 4 5 6 7 8 910111213141516171819202122
funcbestHand(ranks[]int,suits[]byte)string{flush:=truefori:=1;i<5&&flush;i++{flush=suits[i]==suits[i-1]}ifflush{return"Flush"}cnt:=[14]int{}pair:=falsefor_,x:=rangeranks{cnt[x]++ifcnt[x]==3{return"Three of a Kind"}pair=pair||cnt[x]==2}ifpair{return"Pair"}return"High Card"}
1 2 3 4 5 6 7 8 91011121314151617
functionbestHand(ranks:number[],suits:string[]):string{if(suits.every(v=>v===suits[0])){return'Flush';}constcount=newArray(14).fill(0);letisPair=false;for(constvofranks){if(++count[v]===3){return'Three of a Kind';}isPair=isPair||count[v]===2;}if(isPair){return'Pair';}return'High Card';}
1 2 3 4 5 6 7 8 9101112131415161718
implSolution{pubfnbest_hand(ranks:Vec<i32>,suits:Vec<char>)->String{ifsuits.iter().all(|v|*v==suits[0]){return"Flush".to_string();}letmutcount=[0;14];letmutis_pair=false;for&vinranks.iter(){leti=vasusize;count[i]+=1;ifcount[i]==3{return"Three of a Kind".to_string();}is_pair=is_pair||count[i]==2;}(ifis_pair{"Pair"}else{"High Card"}).to_string()}}
1 2 3 4 5 6 7 8 9101112131415161718192021222324
char*bestHand(int*ranks,intranksSize,char*suits,intsuitsSize){boolisFlush=true;for(inti=1;i<suitsSize;i++){if(suits[0]!=suits[i]){isFlush=false;break;}}if(isFlush){return"Flush";}intcount[14]={0};boolisPair=false;for(inti=0;i<ranksSize;i++){if(++count[ranks[i]]==3){return"Three of a Kind";}isPair=isPair||count[ranks[i]]==2;}if(isPair){return"Pair";}return"High Card";}