class Solution { public: int searchInsert(vector<int>& nums, int target) { int left=0; int right=nums.size()-1; int mid; if(nums.size()==0) return 0; while(left < right){ mid = (left + right)/2; if(nums[mid] == target) return mid; else if(nums[mid] < target) left = mid+1; else right = mid-1; } //循环的出口是left = right if(target <= nums[left]) return left; //排在当前元素的位置 else return left+1; //排在当前元素之后 } };