Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Output correct, answer wrong in Leetcode

$
0
0

Was attempting a Leetcode problem where you have to return the indices of two numbers in nums such that they add up to target. Stdout when I printed the return value appeared to be correct, yet the answer is an empty array.

When nums = [2,7,11,15] and target = 9, I expected returnSize to return [0,1] here, yet it was empty. In contrast to similar cases I found online, I did not print out the results. Did I create a copy of returnSize or anything like that?

My code

/** * Note: The returned array must be malloced, assume caller calls free(). */int* twoSum(int* nums, int numsSize, int target, int* returnSize) {    returnSize = malloc(3 * sizeof(int));    for (int i = 0; i < numsSize; i++) {        for (int j = i + 1; j < numsSize; j++) {            if (nums[i] + nums[j] == target) {                *(returnSize)=i;                *(returnSize+1)=j;            }        }    }    //printf("%d and %d", returnSize[0], returnSize[1]);    return returnSize;}

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>