34. Search for a Range

来源:互联网 发布:网络语鹅是什么意思 编辑:程序博客网 时间:2024/06/05 14:47
/*Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in the array, return [-1, -1].For example,Given [5, 7, 7, 8, 8, 10] and target value 8,return [3, 4].思路:    使用二分法找到目标值的左边界使用二分法找到目标值的右边界*//** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */#include <stdio.h>#include <stdlib.h>int* searchRange(int* nums, int numsSize, int target, int* returnSize){*returnSize=2;int *res=(int *)malloc(2*sizeof(int));res[0]=-1;res[1]=-1;int left=0,right=numsSize-1,mid=0;while(left<right){mid=(left+right)>>1;if(nums[mid]<target) left=mid+1;else right=mid;}if(nums[left]!=target)return res;elseres[0]=left;   //存储左边界right=numsSize-1;while(left<right){mid=(left+right + 1)/2;if(nums[mid]>target) right=mid-1;else left=mid;}res[1]=right;   //存储右边界return res;}int main(int argc,char **argv){int array[]={5, 7, 7, 8, 8, 10};int numsSize=sizeof(array)/sizeof(int);int returnSize=0;int target=8;int *p=searchRange(array,numsSize,target,&returnSize);int i=0;for(;i<returnSize;i++)printf("%d\n",p[i]);return 0;}

原创粉丝点击