标准二分搜索,掌握的还不好,还有细节要敲定
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums[0]>target)
return 0;
if(nums[nums.size()-1]<target)
return nums.size();
int n_up=nums.size()-1,n_down=0;
int result;
while(n_down<n_up)
{
result =(n_up+n_down)/2;
if(target>nums[result])
n_down=result+1;
else
n_up=result;
}
return n_up>n_down?n_up:n_down;
}
};