gpt4 book ai didi

c - 为什么我有时会得到不同的结果? [C语言]

转载 作者:行者123 更新时间:2023-11-30 14:48:10 24 4
gpt4 key购买 nike

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

这是我的整个文件:

#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]

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

最佳答案

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;
}

关于c - 为什么我有时会得到不同的结果? [C语言],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50637187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com