35,搜索插入位置
class Solution { public: int searchInsert(vector<int>& nums, int target) { int n=nums.size(); int left=0; int right=n-1; if(target<nums[0]) { return 0; } else if(target>nums[right]) { return n-1; } //下面是二分法处理最后一种情况 else if(target>nums[0]&&target<nums[right]) { while(left<right) { if(target<nums[(left+right)/2]) { right=(left+right)/2; } else if(target>nums[(left+right)/2]) { left=(left+right)/2; } else { return (left+right)/2 ; } } } return left; } };