【Leetcode数组】846. 一手顺子(Java中TreeMap的key排序的使用)

时间:2025-04-04 07:51:14
public class lc846 { public boolean isNStraightHand(int[] hand, int groupSize) { TreeMap<Integer,Integer> count=new TreeMap(); for (int i : hand) { if(count.containsKey(i)==false){ count.put(i,1); }else{ count.replace(i,count.get(i)+1); } } while (count.size()>0){ int begin=count.firstKey(); for(int i=begin;i<=begin+groupSize-1;i++){ if(count.containsKey(i)==true){ if(count.get(i)==1) count.remove(i); else count.replace(i,count.get(i)-1); }else return false; } } return true; } }