链接:846. 一手顺子
题解:
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
sort(hand.begin(), hand.end());
std::unordered_map<int, int> table;
for (auto e : hand) {
++table[e];
}
for (auto begin : hand) {
if (table.find(begin) == table.end()) {
continue;
}
for (int step = 0; step < groupSize; ++step) {
int next = begin + step;
if (table.find(next) == table.end()) {
return false;
}
--table[next];
if (table[next] == 0) {
table.erase(next);
}
}
}
return true;
}
};