# Two Sum Emma Note ## Programming Language: C ### Back to [Leetcode Blind 75 Practice Note](/xh5b5HXPTHGz_jwwymNKlw) [Leetcode: Two Sum](https://leetcode.com/problems/two-sum/) * 一開始我寫的有錯誤: ```c= /** * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* nums, int numsSize, int target, int* returnSize) { returnSize = malloc(2*sizeof(int)); for (int i = 0; i < numsSize; i++) { for (int j = 1; j < numsSize; j++) { if ( (nums[i] + nums[j]) == target ) { returnSize[0] = i; returnSize[1] = j; return returnSize; } } } return NULL; } ``` * 錯誤如下 * Case 1  * Case 2  * Case 3  * 錯誤原因: 1. 搞錯returnSize的意思 * returnSize 的意思是要return多少個int,不是return那個array * 所以要自訂新的array傳回去 ```c= int* returnArray = malloc(2*sizeof(int)); // New array to pass out *returnSize = 2; // Return 2 integers ``` * 正確答案 ```c= /** * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* nums, int numsSize, int target, int* returnSize) { int* returnArray = malloc(2*sizeof(int)); *returnSize = 2; for (int i = 0; i < numsSize; i++) { for (int j = i+1; j < numsSize; j++) { if ( ((nums[i] + nums[j]) == target) && i != j ) { returnArray[0] = i; returnArray[1] = j; return returnArray; } } } return -1; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up