java:
每日一题 学习一下
class Solution { public boolean isNStraightHand(int[] hand, int groupSize) { int n = hand.length; if (hand == null || n % groupSize != 0) { return false; } if (groupSize == 1) { return true; } Arrays.sort(hand); Queuequeue = new linkedList<>(); for (int i = 0; i < n; i++) { if (queue.isEmpty() || queue.peek()[0] == hand[i]){ queue.offer(new int[]{hand[i], 1}); } else if (queue.peek()[0] + 1 == hand[i]) { int[] cur =queue.poll(); cur[0] = hand[i]; if (++cur[1] != groupSize) { queue.offer(cur); } } else { return false; } } return queue.isEmpty(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)