leetcode 35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
思路:
这道题基本没有什么难度,实在不理解为啥还是Medium难度的,完完全全的应该是Easy啊,三行代码搞定的题,只需要遍历一遍原数组,若当前数字大于或等于目标值,则返回当前坐标,如果遍历结束了,说明目标值比数组中任何一个数都要大,则返回数组长度n即可
思路二:二分法
var searchInsert = function(nums, target) {
var l=0;
var r=nums.length;
while(l<r){
var m= Math.floor((l+r)/2);
if(nums[m]>target){
r=m;
}else if (nums[m]<target){
l=m+1;
}else{
r=m;
}
}
return r;
};