leetcode 846. 一手顺子

leetcode 846. 一手顺子,第1张

leetcode 846. 一手顺子

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);
        Queue queue = 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();
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5687259.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存