一手顺子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
// 从小到大排序
sort(hand.begin(), hand.end());
unordered_map<int, int> mp;
// 记录每张牌的个数
for (auto& h : hand) {
mp[h]++;
}
for (int i = 0; i < hand.size(); i++) {
if (mp[hand[i]] == 0) continue;
// 从当前这张牌开始,往后看,能不能凑出一组顺子
for (int x = 0; x < groupSize; x++) {
// 如果缺某一张牌,返回 false
if (mp[hand[i] + x] == 0) {
return false;
}
// 将这张牌剩余的个数减一
mp[hand[i] + x]--;
}
}
return true;
}
};