当数组里的元素没有重复时,因为数组已经是排序好的了,自然想到用二分法。
如果数组里的元素有重复时,magic index可能出现在左侧也可能出现在右侧,这个画图举个例子就知道了。
可以优化的是,无论是在左侧还是右侧,总有一些元素可以直接排除,从而缩短查找时间!
这个是没有重复元素的版本:
package Recursion; import java.util.Arrays; import CtCILibrary.AssortedMethods; /** * A magic index in an array A[l.. .n-l] is defined to be an index such that * A[i] = i. Given a sorted array of distinct integers, write a method to find a * magic index, if one exists, in array A. * * FOLLOW UP What if the values are not distinct? * * 在A[l...n-l]中有一个神奇的下标:它满足A[i] = i 如果给定一个排序过的且里面元素 * 各不相同的数组,写一个方法来找到这个神奇的下标。 * * Follow Up: 假如这个数组的元素有重复的?那该如何找到? * */ public class S9_3 { public static void main(String[] args) { for (int i = 0; i < 1000; i++) { int size = AssortedMethods.randomIntInRange(5, 20); int[] array = getDistinctSortedArray(size); int v2 = findMagicIndexBS(array, 0, array.length-1); // System.out.println(findMagicIndex(array, 0)); if (v2 == -1 && findMagicIndex(array, 0) != -1) { int v1 = findMagicIndex(array, 0); System.out.println("Incorrect value: index = -1, actual = " + v1 + " " + i); System.out.println(AssortedMethods.arrayToString(array)); break; } else if (v2 > -1 && array[v2] != v2) { System.out.println("Incorrect values: index= " + v2 + ", value " + array[v2]); System.out.println(AssortedMethods.arrayToString(array)); break; } } } public static int findMagicIndex(int[] A, int start){ if(start >= A.length){ return -1; } if(A[start] == start){ return start; } return findMagicIndex(A, start+1); } // 在没有重复递增的数组中找到Magic index public static int findMagicIndexBS(int[] A, int low, int high){ if(low > high){ return -1; } int mid = low + (high-low)/2; if(A[mid] == mid){ // 找到了 return mid; } if(A[mid] < mid){ // 要在右侧找 low = mid+1; }else{ // 要在左侧找 high = mid - 1; } return findMagicIndexBS(A, low, high); } /* Creates an array that is distinct and sorted */ private static int[] getDistinctSortedArray(int size) { int[] array = AssortedMethods.randomArray(size, -1 * size, size); Arrays.sort(array); for (int i = 1; i < array.length; i++) { if (array[i] == array[i-1]) { array[i]++; } else if (array[i] < array[i - 1]) { array[i] = array[i-1] + 1; } } return array; } }
这个是有重复元素的版本:
package Recursion; import java.util.Arrays; import CtCILibrary.AssortedMethods; public class S9_3_2 { /* Creates an array that is sorted */ public static int[] getSortedArray(int size) { int[] array = AssortedMethods.randomArray(size, -1 * size, size); Arrays.sort(array); return array; } public static void main(String[] args) { for (int i = 0; i < 1000; i++) { int size = AssortedMethods.randomIntInRange(5, 20); int[] array = getSortedArray(size); int v2 = findMagicIndexBS2(array, 0, array.length-1); if (v2 == -1 && magicSlow(array) != -1) { int v1 = magicSlow(array); System.out.println("Incorrect value: index = -1, actual = " + v1 + " " + i); System.out.println(AssortedMethods.arrayToString(array)); break; } else if (v2 > -1 && array[v2] != v2) { System.out.println("Incorrect values: index= " + v2 + ", value " + array[v2]); System.out.println(AssortedMethods.arrayToString(array)); break; } } } public static int findMagicIndexBS2(int[] A, int low, int high){ if(low > high){ return -1; } int mid = low + (high-low)/2; if(A[mid] == mid){ // 找到了 return mid; } int index = -1; // 先搜索左侧,观察规律可发现可跳过一些数,直接从A[mid]开始往前 index = findMagicIndexBS2(A, low, Math.min(mid-1, A[mid])); if(index == -1){ index = findMagicIndexBS2(A, Math.max(mid+1, A[mid]), high); } return index; } public static int magicSlow(int[] array) { for (int i = 0; i < array.length; i++) { if (array[i] == i) { return i; } } return -1; } }
作者:hellobinfeng 发表于2013-11-27 1:01:09 原文链接
阅读:88 评论:0 查看评论