class Solution {
public:
int binary_search(vector<int>& nums,int b,int e,int p){
if(b==e){
if(nums[b]<p){
return b+1;
}
else{
return b;
}
}
else{
int mid=(b+e)/2;
if(p<=nums[mid]){
return binary_search(nums,b,mid,p);
}
else{
return binary_search(nums,mid+1,e,p);
}
}
}
int searchInsert(vector<int>& nums, int target) {
return binary_search(nums,0,nums.size()-1,target);
}
};
LeetCode - 35 Search Insert Position
最新推荐文章于 2015-09-21 15:35:19 发布