TheAlgorithms-C/leetcode/src/35.c
Tanyapohn Pathummasutr b1b24e69c9 Remove whitespace
2019-10-05 23:07:21 +07:00

26 lines
626 B
C

int searchInsert(int* nums, int numsSize, int target){
int low = 0, high = numsSize - 1, mid;
while (low <= high) {
mid = low + (high - low) / 2;
if (target > nums[mid])
low = mid + 1;
else if (target < nums[mid])
high = mid - 1;
else
return mid;
}
return low;
}
/* Recursive version */
int searchInsert(int* nums, int numsSize, int target){
int idx = numsSize - 1;
if(numsSize>0){
if (target > nums[idx]){
return numsSize;
}
return searchInsert(nums, numsSize - 1, target);
}
return 0;
}