这个题没想出来,看的答案get
1.这道题使用二分法,没有想到,当我看到答案,有点恍然大悟的感受,这就是把学校学的知识运用出来了,实习到如今也有两年了,这是第一次使用二分法,一直都知道二分法是什么原理是什么,可是没有一次运用。io
2.知道二分法这题就容易了,取极值得下标,循环迭代,若是一直不相等,直到low=height返回low下标class
class Solution {
public int searchInsert(int[] nums, int target) {
int low = 0,height = nums.length - 1;
while(low <= height){
int tag = (low + height) / 2;
if(nums[tag] == target){
return tag;
}else if(nums[tag] < target){
low = tag + 1;
}else{
height = tag - 1;
}
}
return low;
}
}原理