goe*_*goe 1 java arrays long-integer
我有一个包含这些数字的长数组:
long[] = {1,2,3,5,6,7};
Run Code Online (Sandbox Code Playgroud)
4请注意,缺少该内容。如果存在或不存在此类间隙,测试该阵列的最佳方法是什么?
小智 5
如果你保证数组是有序的,没有任何重复,那么你可以在 O(1) 中检查这一点
我认为这段代码应该在这种特定情况下工作:)
//assume that given array is ordered and has no duplicated value
long[] myarray = {5,6,7}; //no gap
long[] myarray1 = {1,2,4}; //has gap
long[] myarray2 = {10,11,12,13,14,15}; //no gap
//return true if has gap
//return false if no gap
//throw null-pointer if empty
public static boolean checkIfHasGap(long[] array) {
if (array.length == 0) {
throw new NullPointerException("Given Array is empty");
} else {
return array[0] + array.length != array[array.length - 1] + 1;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5269 次 |
| 最近记录: |