0
点赞
收藏
分享

微信扫一扫

Leetcode 34. 在排序数组中查找元素的第一个和最后一个位置

幺幺零 2022-05-10 阅读 160

Leetcode 34. 在排序数组中查找元素的第一个和最后一个位置

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* searchRange(int* nums, int numsSize, int target, int* returnSize){
    *returnSize = 2;
    int* array = (int*)malloc(sizeof(int)*2);
    array[0] = -1;
    array[1] = -1;
    if(nums == NULL)
    {
        return array;
    }
    int left = 0;
    int right = numsSize-1;
    int flag = 1;
    while(left <= right && array[0] == array[1] && array[0] == -1)
    {
        while(left <= right && nums[left] != target)
        {
            left++;
        }
        if(left < numsSize && nums[left] == target)
        array[0] = left;
        while(left <= right && nums[right] != target)
        {
            right--;
        }
        if(right >= 0 && nums[right] == target)
        array[1] = right;
    }
    return array;
}
举报

相关推荐

0 条评论