The links to the compiled collections are here.
Leetcode, PAT, CCF CSP real questions C/C++ full-score answers over the years have been carefully compiled collections
In fact, it is the first time to do the question of Likou, and I am a bit confused. I don't know what the input and output format of the question means.
Looking at other people's answers, there is no scanf and printf. After a while, I found out that it was written to the interface. Maybe this is closer to the actual work, hahaha
#include <stdio.h>
#include <stdlib.h>
int binarySearch(int* nums, int numsSize, int target)
{
int id,l=0,r=numsSize-1;
while(l<=r)
{
id = (l+r)/2;
if(nums[id]==target)
return id;
else if(nums[id]>target)
r = id-1;
else
l = id+1;
}
return -1;
}
int* searchRange(int* nums, int numsSize, int target, int* returnSize)
{
int id = binarySearch(nums,numsSize,target);
int* p = (int*)malloc(sizeof(int)*2);
*returnSize = 2;
if(id==-1)
{
p[0]=-1;
p[1]=-1;
}else
{
p[0]=id;
p[1]=id;
while(--p[0]>=0 && nums[p[0]]==target);
p[0]++;
while(++p[1]<numsSize && nums[p[1]]==target);
p[1]--;
}
return p;
}