贴原题:
Given a sorted array and a target value, return the index if the解析:
target is found. If not, return the index where it would be if it were
inserted in order.
【leetcode 35. Search Insert Position(C语言,查询插入位置)19】You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7→ 4
[1,3,5,6], 0 → 0
本题是给出一个有序数组(从小到大排列)和一个目标值,然后找出该目标值在数组中的位置,如果没有该值,就返回该数所应该在的位置。
那么我们只需要进行一次遍历,如果查找到一个数大于或者等于目标值,即返回该数所在位置即可。
贴代码:
int searchInsert(int* nums, int numsSize, int target) {
for(int i=0;
i=target)
{
return i;
}
}
return numsSize;
}