You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求排查LeetCode题目Search for a Range的代码Bug

Hey there! Let's break down the common bugs that tend to creep into unoptimized linear-scan solutions for LeetCode's Search for a Range problem. Since your code snippet got cut off, I’ll focus on the most frequent issues developers run into with this approach—these are almost certainly the culprits if your solution is failing test cases.

Common Bugs & Fixes

1. Forgetting to set *returnSize to 2

This is one of the most overlooked requirements. LeetCode expects you to use the returnSize pointer to tell the caller how big your returned array is. If you skip this, the caller might access out-of-bounds memory or handle the result incorrectly.

Bad Code Snippet:

int* searchRange(int* nums, int numsSize, int target, int* returnSize) {
    int* result = malloc(2 * sizeof(int));
    // ... rest of your logic ...
    return result; // Oops, never updated returnSize!
}

Fix:
Add this line right after initializing your result (before any early returns):

*returnSize = 2;

The returned array is always size 2, whether the target is found or not.

2. Mishandling the "target not found" case

When the target isn’t present in nums, you need to return [-1, -1]. Many developers either forget to initialize their start/end variables to -1, or fail to assign these values to the result array.

Bad Code Snippet:

int* searchRange(int* nums, int numsSize, int target, int* returnSize) {
    *returnSize = 2;
    int* result = malloc(2 * sizeof(int));
    int start, end; // Uninitialized—garbage values!
    for (int i = 0; i < numsSize; i++) {
        if (nums[i] == target) {
            if (start == -1) start = i;
            end = i;
        }
    }
    return result; // Never assigned start/end to result!
}

Fix:
Initialize start and end to -1 upfront, then assign them to the result array before returning:

int start = -1, end = -1;
// ... loop logic ...
result[0] = start;
result[1] = end;

3. Incorrectly updating start/end positions

It’s easy to mess up the logic for tracking the first and last occurrence of the target. A common mistake is overwriting the start position every time you see the target, or failing to update the end position repeatedly.

Bad Code Snippet:

for (int i = 0; i < numsSize; i++) {
    if (nums[i] == target) {
        start = i; // This makes start the LAST occurrence, not the first
        end = i;
    }
}

Fix:
Only set start once (the first time you encounter the target), and update end every time you find the target:

for (int i = 0; i < numsSize; i++) {
    if (nums[i] == target) {
        if (start == -1) { // Only set start if we haven't found the first occurrence yet
            start = i;
        }
        end = i; // Update end every time we see the target
    }
}

4. Memory allocation errors

Using the wrong size for malloc leads to undefined behavior. You need to allocate space for 2 integers, not just 1.

Bad Code Snippet:

int* result = malloc(sizeof(int)); // Only enough space for 1 int—writing to result[1] will crash or corrupt memory

Fix:
Allocate space for 2 integers, and optionally add a check for malloc failure (LeetCode usually doesn’t test this, but it’s good practice):

int* result = malloc(2 * sizeof(int));
if (result == NULL) {
    *returnSize = 0;
    return NULL;
}
Correct Unoptimized Solution Example

Here’s a complete version that avoids all the above bugs:

/**
 * 返回大小为*returnSize的数组。
 * 注意:返回的数组必须通过malloc分配内存,调用者需调用free()释放。
 */
int* searchRange(int* nums, int numsSize, int target, int* returnSize) {
    *returnSize = 2;
    int* result = malloc(2 * sizeof(int));
    if (result == NULL) {
        *returnSize = 0;
        return NULL;
    }
    
    int start = -1, end = -1;
    for (int i = 0; i < numsSize; i++) {
        if (nums[i] == target) {
            if (start == -1) {
                start = i;
            }
            end = i;
        }
    }
    
    result[0] = start;
    result[1] = end;
    return result;
}

内容的提问来源于stack exchange,提问作者1MinLeft

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:47:54