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

LeetCode提交代码输出正确却判错,求C语言回溯法问题排查

LeetCode提交失败排查求助

我的代码在VS Code和在线GDB编译器中运行输出正确,但提交至LeetCode时始终被判定为不符合预期输出。以下是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int *count_op;
int k = 0;

void backTrack(int *nums, int numsSize, int x, int i, int j, int counter)
{
    if (x == 0)
    {
        count_op[k++] = counter;
        return ;
    }
    else if (x < 0)
    {
        return ;
    }
    if (i < numsSize && j >= 0)
    {
        backTrack(nums, numsSize, x - nums[i], i + 1, j, counter + 1);
        backTrack(nums, numsSize, x - nums[j], i, j - 1, counter + 1);
    }
}

int minOperations(int* nums, int numsSize, int x)
{
    int z = 0;
    int min = -1;
    while (z < numsSize)
    {
        if (nums[z] < 1 || nums[z] > pow(10,4))
            return (-1);
        z++;
    }
    z = 0;
    if (numsSize >= 1 && numsSize <= pow(10,5) && x >= 1 && x <= pow(10,9))
    {
        count_op = malloc(sizeof(int) * pow(2, numsSize - 1));
        while (z < numsSize)
        {
            count_op[z] = -1;
            z++;
        }
        backTrack(nums, numsSize, x, 0, numsSize - 1, 0);
        min = count_op[0];
        z = 0;
        while (z < numsSize && count_op[z] > 0)
        {
            if (min > count_op[z])
                min = count_op[z];
            z++;
        }
    }
    return (min);
}

int main(void)
{
    int nums[] = {3,2,20,1,1,3};
    int min = minOperations(nums, 6, 10);
    printf("%d\n", min);
}

相关参考信息:

  • LeetCode问题描述及判定结果截图
  • VS Code运行输出结果截图
  • 在线GDB编译器运行输出结果截图

我是编程新手,第一次使用LeetCode,完全不知道该如何排查问题,恳请各位提供帮助!

内容的提问来源于stack exchange,提问作者Anas Jawad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 05:46:19