#include<iostream>
#include<queue>
#include<unordered_map>
#include<vector>
using namespace std;
//这是错的
bool isNStraightHand_and_isWrong(vector<int>& hand, int groupSize) {
int len = hand.size();
if(len % groupSize != 0){
return false;
}
vector<int> arr(groupSize);
for(int i :hand){
arr[i%groupSize]++;
}
for(int j = 0;j < groupSize-1;j++){
if(arr[j] != arr[j+1]){
return false;
}
}
return true;
}
bool isNStraightHand(vector<int>&hand, int groupSize){
int len = hand.size();
unordered_map<int,int>cnt;
// 只需要导入队列的头文件即可
priority_queue<int,vector<int>,greater<int>>Q;
for(int i:hand){
cnt[i]++;
//压入优先队列
Q.push(i);
}
while(! Q.empty()){
int c = Q.top();
Q.pop();
//取出的一手牌中最小的牌,如果没有就 continue
if(cnt[c] == 0)continue;
//有牌的话就判断是否连续
for(int x = 0;x < groupSize;x++){
if(cnt[c + x] == 0) return false;
//如果有重复的牌就要--
cnt[c+x]--;
}
}
return true;
}
int main(){
vector<int>test1 = {1,2,3,6,2,3,4,7,8};
vector<int>test2 = {1,2,3,4,5};
vector<int>test3 = {8,10,12} ;
int groupSize1 = 3;
int groupSize2 = 4;
int groupSize3 = 3;
cout << isNStraightHand_and_isWrong(test1,groupSize1) << endl;
cout << isNStraightHand_and_isWrong(test2,groupSize2) << endl;
cout << isNStraightHand_and_isWrong(test3,groupSize3) << endl;
cout << isNStraightHand(test1,groupSize1) << endl;
cout << isNStraightHand(test2,groupSize2) << endl;
cout << isNStraightHand(test3,groupSize3) << endl;
return 0;
}
leetcode 一手顺子
最新推荐文章于 2024-11-01 15:19:21 发布