难度:简单
一个入门的算法,代码如下:
class Solution {
public int search(int[] nums, int target) {
int h = 0;
int w = nums.length-1;
int min = (h+w)/2;
while (true){
if (nums[min] == target){
return min;
}else if (nums[min] < target){
h = min + 1;
}else {
w = min -1;
}
min = (h+w)/2;
if (h>w){
return -1;
}
}
}
}