排序之后模拟,先统计零的个数,然后从非零元素开始,如果每个元素不等于他的前ji,就用 0 补,补不完就返回false
class Solution {
public:
bool isContinuous( vector<int> numbers ) {
int len = numbers.size();
if(len != 5)return false;
sort(numbers.begin(), numbers.end());
int cnt = 0;
int prev = -1;
while(cnt < 5 && numbers[cnt] == 0)cnt ++;
for(int i = cnt; i < 5; i ++){
if(prev!=-1){
while(cnt >= 0 && numbers[i] != prev + 1){
cnt --;
prev = prev + 1;
}
if(cnt < 0)return false;
}
prev = numbers[i];
}
return true;
}
};