分割数组为连续子序列

//测试中发现nums元素值都在正负10000以内
    public boolean isPossible(int[] nums) {
        int[] counts = new int[20000];
        int[] need = new int[20000];

        for (int i = 0; i < nums.length; i ++) {
            //将所有负数转为正数
            nums[i] += 10000;
            counts[nums[i]] ++;
        }

        for (int n : nums) {
            if (counts[n] == 0) continue;

            if (need[n] > 0){
                need[n] --;
                need[n + 1] ++;
            } else if (counts[n + 1] > 0 && counts[n + 2] > 0){
                counts[n + 1] --;
                counts[n + 2] --;
                need[n + 3] ++;
            } else return false;
            counts[n] --;
        }
        return true;
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容