1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| public class Test__ {
public static void main(String[] args) { int a[] = {1, 2, 4, 6, 8, 10, 12}; int b[] = {2, 4, 6, 8, 10, 12};
System.out.println(findDifference(a, b));
}
public static int findDifference(int a[], int b[]) { int start, mid, end; if (a == null || b == null || a.length != b.length + 1) return -1;
end = a.length - 1; start = 0;
while (start <= end) {
if (start == end) break; mid = (start + end) / 2; if (a[mid] == b[mid]) { start = mid + 1; } else { end = mid - 1; } }
int i = 0, j = 0; for (i = 0; i < a.length && j < a.length; i++, j++) { if (i == start) { j--; continue; } else if (a[i] != b[j]) { System.out.println("数组不合法"); break; } } if (i == a.length) return start;
return -1; }
}
|