Leetcode“Two Sums”的C解决方案有运行时错误

时间:2015-07-05 03:41:12

标签: c runtime-error

我刚刚完成了Leetcode“Two Sums”的C解决方案。但该网站称“运行时错误”,没有说明哪行代码是错误的。以下代码在Xcode中完美运行。我的代码有什么问题吗?

int* twoSum(int* nums, int numsSize, int target);

int main(int argc, const char * argv[]) {
    int nums[] = { 1, 2, 4, 8, 16};
    int numsSize = 5;
    int target = 10;
    int *answer = twoSum(nums, numsSize, target);

    printf("index1 = %d, index2 = %d\n", answer[0], answer[1]);

    return 0;
}

struct bucketLayer {
    int data;
    int index;
    struct bucketLayer* ptr;
};

struct result {
    int found;
    int index;
};

struct bucketLayer *addData(int data, int index, struct bucketLayer *targetPtr);
struct result findData(int data, int firstIndex, struct bucketLayer *targetPtr);
struct bucketLayer *freeBucket(struct bucketLayer *bucketPtr);

int* twoSum(int* nums, int numsSize, int target) {
    struct bucketLayer *buckets[target];

    int *answer = (int *)malloc(2 * sizeof(int));

    for (int i = 0; i < numsSize; i++) {
        buckets[nums[i] % target] = addData(nums[i], i, buckets[nums[i] % target]);
    }

    for (int i = 0; i < numsSize - 1; i++) {
        struct result findResult = findData(target - nums[i], i, buckets[target - nums[i] % target]);
        if (findResult.found) {
            if (findResult.index > i) {
                answer[0] = i+1;
                answer[1] = findResult.index + 1;
                return answer;
            } else {
                answer[0] = findResult.index + 1;
                answer[1] = i + 1;
                return answer;
            }
        }
    }

    for (int i = 0; i < target; i++) {
        buckets[i] = freeBucket(buckets[i]);
    }

    return answer;
}

struct bucketLayer *addData(int data, int index, struct bucketLayer *targetPtr) {
    if (targetPtr == NULL) {
        targetPtr = (struct bucketLayer *)malloc(sizeof(struct bucketLayer));
        targetPtr->data = data;
        targetPtr->index = index;
        targetPtr->ptr = NULL;
    } else {
        targetPtr->ptr = addData(data, index, targetPtr->ptr);
    }

    return targetPtr;
}

struct result findData(int data, int firstIndex, struct bucketLayer *targetPtr) {
    struct result findResult;
    if (targetPtr == NULL) {
        findResult.found = 0;
        return findResult;
    } else {
        if (targetPtr->data == data && targetPtr->index != firstIndex) {
            findResult.found = 1;
            findResult.index = targetPtr->index;
            return findResult;
        } else {
            return findData(data, firstIndex, targetPtr->ptr);
        }
    }
}

struct bucketLayer *freeBucket(struct bucketLayer *bucketPtr) {
    if (bucketPtr != NULL) {
        bucketPtr->ptr = freeBucket(bucketPtr->ptr);
        free(bucketPtr);
    }
    return bucketPtr;
}

1 个答案:

答案 0 :(得分:2)

仔细查看代码之后,通过使用指向struct bucketLayer的可变长度数组引入问题。在该实例中,当存在可用的静态指针时,指针未初始化并导致addData函数错误地处理指针。只需手动初始化即可:

struct bucketLayer *buckets[target];

for (int i = 0; i < target; i++)
    buckets[i] = NULL;

int *answer = malloc (2 * sizeof *answer);

for (int i = 0; i < numsSize; i++) {
    buckets[nums[i] % target] = addData(nums[i], i, buckets[nums[i] % target]);
}

addData分配也可以简化为:

 targetPtr = malloc (sizeof *targetPtr);