/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* searchRange(int* nums, int numsSize, int target, int* returnSize){
*returnSize = 2;
int *res = calloc(2, sizeof(int));
res[0] = -1;
res[1] = -1;
if (numsSize == 0) {
return res;
}
int Index1 = -1, Index2 = -1;
for (int i=0; i<numsSize; i++) {
if (nums[i] < target)
continue;
else if (nums[i] == target) {
if (Index1 == -1) {
Index1 = i;
}
if ( i > Index2 ) {
Index2 = i;
}
} else if (nums[i] > target)
break;
}
res[0] = Index1;
res[1] = Index2;
return res;
}
用时4ms,写完后看了看评论区发现可以用二分法寻找target的值,时间复杂度为LogN