您好,登錄后才能下訂單哦!
Arrays.copyOfRange(int[] arr,int開(kāi)始下標(biāo),int結(jié)束下標(biāo))//左閉右開(kāi)
System.arraycopy(int[] source,int開(kāi)始下標(biāo),int[] dest,int開(kāi)始下標(biāo),int拷貝多少個(gè))
這里是手動(dòng)拷貝:
public static int[] copyOf(int[] original, int newLength) {
int[] dest = new int[newLength];
int length = original.length <= newLength ? original.length : newLength;
for (int i = 0; i < length; i++) {
dest[i] = original[i];
}
return dest;
}
mid作為分水嶺, 不斷地縮小范圍
// 前提需要數(shù)組是有序的
public static int binarySearch(int[] a, int v) {
int left = 0;
int right = a.length;
while (left < right) {
int mid = (left + right) / 2;
if (v == a[mid]) {
return mid;
} else if (v < a[mid]) {
right = mid;
} else {
left = mid + 1;
}
}
return -1;
}
public static int binarySearch3(int[] a, int v) {
int left = 0;
int right = a.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (v == a[mid]) {
return mid;
} else if (v < a[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。