题目链接:搜索插入位置
太简单了,直接上代码:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int pivot, left = 0, right = nums.size() - 1;
while (left <= right) {
pivot = left + (right - left) / 2;
if (nums[pivot] == target) return pivot;
else if (nums[pivot] > target) right = pivot - 1;
else left = pivot + 1;
}
return left;
}
};