题目
https://leetcode-cn.com/problems/hand-of-straights/
思路
这个哈希表就用得很巧妙啊啊啊
代码
class Solution {
public boolean isNStraightHand(int[] hand, int groupSize) {
Arrays.sort(hand);//每组第一个数都是这组最小的,所以我们正序排列找各组起点
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<hand.length;i++){
map.put(hand[i],map.getOrDefault(hand[i],0)+1);
}
for(int i=0;i<hand.length;i++){
if(map.get(hand[i])<=0){//说明这个数已经被其他组用完了,不能再用来当起点
continue;
}
for(int j=0;j<groupSize;j++){
int num=hand[i]+j;
if(!map.containsKey(num)||map.get(num)<=0){//没有连续的
return false;
}
map.put(num,map.getOrDefault(num,0)-1);
}
}
return true;
}
}