标签:
import java.util.Arrays; public class Solution { public boolean isContinuous(int [] numbers) { if(numbers.length!=5) return false; Arrays.sort(numbers); int cnt0 = 0; int cntNeed = 0; for(int i=0; i<5; i++){ if(numbers[i]==0){ cnt0++; }else{ if(i<4){ if(numbers[i+1] == numbers[i]) return false; cntNeed += numbers[i+1]-numbers[i]-1; } } } if(cntNeed > cnt0) return false; return true; } //必须满足两个条件: 1. 除0外没有重复的数 2. max - min < 5 public boolean isContinuous2(int[] numbers){ if(numbers.length!=5) return false; Arrays.sort(numbers); int count = 0; for(int i=0; i<numbers.length; i++){ if(numbers[i]==0){ count++; }else{ if(i<4 && (numbers[i+1]==numbers[i])){ return false; } } } if(count>=4) return true; if(numbers[numbers.length-1] - numbers[count] <5) return true; return false; } }
标签:
原文地址:http://www.cnblogs.com/hesier/p/5586412.html