//测试中发现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;
}
网友评论