class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int start = 0, end = nums.size()-1;
if (nums[end] < target) {
return end + 1;
}
if (nums[start] > target) {
return start;
}
while (start <= end) {
int mid = start + (end - start) /2;
if (nums[mid] == target ||
(nums[mid] > target && nums[mid - 1] < target)) {
return mid;
}
if (nums[mid] < target && nums[mid + 1] > target) {
return mid + 1;
}
if (nums[mid] > target) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
};