Runtime error in my C solution of Two Sums


  • 0
    Z

    I'm new to Leetcode, and I just finished question 1 of Algorithms (two sums). After I submitted the code, the website says runtime error. But I can't find any error with Xcode. Is there anything wrong in my code?

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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.