为什么我偶尔会得到不同的结果? [C语言]

时间:2018-06-01 06:20:19

标签: c malloc

  

给定一个整数数组,返回两个数字的索引   他们加起来就是一个特定的目标。

这是我的整个文件:

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

int* twosum(int* nums, int nums_size, int sum);

int main(int argc, char** argv) {
    if (argc != 3) {
        printf("Two arguments required.\n");
        return EXIT_FAILURE;
    }

    int nums_buffer_size = 10;
    int nums_actual_size = 0;
    int* nums = malloc(sizeof(int) * nums_buffer_size);

    char* charred_num = NULL;
    char delim = ',';

    while ((charred_num = strsep(&argv[1], &delim)) != NULL) {
        nums[nums_actual_size] = atoi(charred_num);
        ++nums_actual_size;
        if (nums_buffer_size <= nums_actual_size) {
            nums_buffer_size = nums_buffer_size * 2;
            nums = realloc(nums, sizeof(int) * nums_buffer_size);
        }
    }

    int* indices = twosum(nums, nums_actual_size, atoi(argv[2]));
    printf("[%d, %d]\n", indices[0], indices[1]);
    free(indices);
    free(nums);

    return EXIT_SUCCESS;
}

typedef struct int_pair {
    int value;
    int index;
} int_pair_t;

int get_other_index(int_pair_t* pairs, int pairs_size, int value, int index) {
    for (int i = 0; i < pairs_size; ++i) {
        if (pairs[i].value == value && pairs[i].index != index) {
            return pairs[i].index;
        }
    }

    return -1;
}

void append_pair(int_pair_t* pairs, int* pairs_size, int value, int index) {
    pairs[*pairs_size].value = value;
    pairs[*pairs_size].index = index;
    *pairs_size = *pairs_size + 1;
}

int* twosum(int* nums, int nums_size, int sum) {
    int* indices = malloc(sizeof(int) * 2);
    indices[0] = -1;
    indices[1] = -1;

    int_pair_t* pairs = malloc(sizeof(int_pair_t) * nums_size);
    int pairs_size = 0;
    for (int i = 0; i < nums_size; ++i) {
        append_pair(pairs, &pairs_size, nums[i], i);
    }

    for (int i = 0; i < nums_size; ++i) {
        int other_index = get_other_index(pairs, pairs_size, sum - nums[i], i);
        if (other_index != -1) {
            indices[0] = i;
            indices[1] = other_index;
            break;
        }
    }

    free(pairs);
    return indices;
}

使用以下命令执行时:

./a.out 12,12,1,4,8,3,10 14

我希望

[3, 6]

但偶尔会得到

[-1, -1]

我认为这个错误与内存(错误)管理有关,但我很难找到它。如果有人能提供一些见解,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

https://leetcode.com/problems/two-sum/description/

simlpe解决功能:

//
//  main.m
//  HFCDemo
//
//  Created by HF on 2018/9/5.
//  Copyright © 2018年 HF. All rights reserved.
//

#import <Foundation/Foundation.h>

/**
 * Note: The returned array must be malloced, assume caller calls free().
 * int *array = (int *)malloc(sizeof(int) * 2);
 */
int* twoSum(int* nums, int numsSize, int target) {
    int oneIndex = 0;
    int twoIndex = 0;
    for (int i = 0;i < numsSize; i ++) {
        int one = nums[i];
        int two = target - one;
        for (int j = 0; j < numsSize; j ++) {
            if (j > i && nums[j] == two) {
                oneIndex = i;
                twoIndex = j;
                break;
            }
        }
    }
    //要求必须动态分配空间数组
    int *array = (int *)malloc(sizeof(int) * 2);
    array[0] = oneIndex;
    array[1] = twoIndex;
    return array;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!\n");
        int numSize = 4;
        int nums[4] = {2, 7, 11, 15};
        int target = 9;
        int *array = twoSum(nums, numSize, target);
        printf("%d %d\n",array[0],array[1]);
        free(array);
    }
    return 0;
}