一个List存数字
先对List中的先字排序
如果list内存的数字是连续的如{1,2,3,4,5,6,7,8,9}刚返回数字为list里的最大数字加1返回数字就为10
如果list内存的数字不是连续的如{1,2,4,7,8,9}则返回数字应该为5
请高新指点,谢谢!
------解决方案--------------------
- Java code
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; }}
------解决方案--------------------
- HTML code
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); }}