加班刷水题
cpp
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if (target <= nums[0]) return 0;
if (target > nums[nums.size() - 1]) return nums.size();
int l = 0;
int r = nums.size() - 1;
while (l <= r) {
int m = (l + r) / 2;
if (target == nums[m]) return m;
if (target > nums[m]) {
if (target < nums[m + 1]) return m + 1;
l = m + 1;
} else {
r = m - 1;
}
}
return -1;
}
};