题目描述:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
思路解析:参考:https://www.cnblogs.com/springfor/p/3858140.html
代码:
public class Solution {
public int search(int[] A, int target) {
if(A==null || A.length==0)
return -1;
int low = 0;
int high = A.length-1;
while(low<=high){
int mid = (high+low)/2;
if(targetA[mid]){
if(A[mid]>A[low]){//左侧排好序了
low = mid+1;
}else{//右侧排好序了
if(target>A[high]){
high = mid-1;
}else{
low = mid+1;
}
}
}else{
return mid;
}
}
return -1;
}
}
题目描述:
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
思路解析:摘自https://www.nowcoder.com/questionTerminal/9f3231a991af4f55b95579b44b7a01ba
采用二分法解答这个问题,
mid = low + (high - low)/2
需要考虑三种情况:
(1)array[mid] > array[high]:
出现这种情况的array类似[3,4,5,6,0,1,2],此时最小数字一定在mid的右边。
low = mid + 1
(2)array[mid] == array[high]:
出现这种情况的array类似 [1,0,1,1,1] 或者[1,1,1,0,1],此时最小数字不好判断在mid左边
还是右边,这时只好一个一个试 ,
high = high - 1
(3)array[mid] < array[high]:
出现这种情况的array类似[2,2,3,4,5,6,6],此时最小数字一定就是array[mid]或者在mid的左
边。因为右边必然都是递增的。
high = mid
注意这里有个坑:如果待查询的范围最后只剩两个数,那么mid 一定会指向下标靠前的数字
比如 array = [4,6]
array[low] = 4 ;array[mid] = 4 ; array[high] = 6 ;
如果high = mid - 1,就会产生错误, 因此high = mid
但情形(1)中low = mid + 1就不会错误
代码:
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
int low =0;
int high = array.length-1;
while(lowarray[high]){
low=mid+1;
}else if(array[mid]==array[high]){
high=high-1;
}else{
high=mid;
}
}
return array[low];
}
}