先排序,然后用大小王尽可能的填补两数间的间距。
import java.util.Arrays; public class Solution { public boolean isContinuous(int[] numbers) { if (numbers == null || numbers.length < 5) { return false; } Arrays.sort(numbers); int king = 0; for (int i = 0; i < numbers.length; i++) { if (numbers[i] != 0) { king = i; break; } } for (int i = king + 1; i < numbers.length; i++) { if (numbers[i] - numbers[i - 1] == 0) { // 前后数相同 return false; } else if (numbers[i] - numbers[i - 1] > 1) { if (king < numbers[i] - numbers[i - 1] - 1) { // 大小王填补不了两数间的间距 return false; } king -= numbers[i] - numbers[i - 1] - 1; } } return true; } }