标签:interview
int find(int[] ints, int low, int high, int t) { if (low > high) return -1; // not found int mid = (low + high) / 2; if (ints[mid] == t) return mid; // found if (ints[mid] < t) { // if max/min point is left side, t is in [mid-high] or [left-max] // if max/min point is right side, t is in [mid-max] if (isMPointLeft(ints, low, mid)) { if (ints[high] < t) { return find(ints, low, mid - 1); } else { return find(ints, mid + 1, high); } } else { return find(ints, mid + 1, high); } } else { // if max/min point is left side, t is in [min-mid] // if max/min point is right side, t is in [low - mid] or [min - high] if (isMPointLeft(ints, low, mid)) { return find(ints, left, mid - 1); } else { if (ints[low] > t) { return find(ints, mid + 1, high); } else { return find(ints, low, mid - 1); } } } } boolean isMPointLeft(int[]ints, int low, int mid) { return ints[low] > ints[mid]; }
标签:interview
原文地址:http://7371901.blog.51cto.com/7361901/1589690