日期:2014-05-20 浏览次数:20688 次
import java.util.Arrays; public class Test { public static void main(String[] args) { int[] a = {1,2,3,4,5,6,7,8,9}; int next1 = nextContinuous(a); System.out.println(next1); int[] b = {1,2,3,5,6,7,8,9}; int next2 = nextContinuous(b); System.out.println(next2); } private static int nextContinuous(int[] nums) { Arrays.sort(nums); int k = nums[0]; for(int i = 0; i < nums.length; i++, k++) { if(nums[i] != k) { return k; } } return k; } }
------解决方案--------------------
import java.util.Arrays; class Discontinuity { public static void getDiscontinuity(int[] array) { Arrays.sort(array); int i = 0, temp = array[0]; for (; i < array.length; i++, temp++) { if (array[i] != temp) { break; } } System.out.println(temp); } public static void main(String[] args) { int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] array2 = { 1, 2, 3, 5, 6, 7, 8, 9 }; getDiscontinuity(array1); getDiscontinuity(array2); } }