美文网首页
分割数组为连续子序列

分割数组为连续子序列

作者: DaiMorph | 来源:发表于2019-10-06 16:37 被阅读0次
//测试中发现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;
    }

相关文章

网友评论

      本文标题:分割数组为连续子序列

      本文链接:https://www.haomeiwen.com/subject/kpiupctx.html