Nums given an integer array and the target value of a target, can you please find out and in the array for the target values of the two integers, and return to their array subscript,
You can assume that each input only corresponding to an answer, however, cannot use the same element in an array twice,
Example:
A given nums=[2, 7, 11, 15], target=9
Because nums nums [0] + [1]=2 + 7=9
So back to [0, 1]
Link: https://leetcode-cn.com/problems/two-sum
My answer:
/* ** Note: The returned array must be malloced, assume caller calls free ().
*/
Nums int * twoSum (int * and an int numsSize, int target, int * returnSize)
{
//distribution of LST array memory use malloc function malloc (sizeof (int) * 2)
Int * LST=(int) malloc (sizeof (int) * 2);
//two traverse cycle
for (int i=0; I{
For (int j=I + 1; J{
//whether I j corresponds to subscript arrays together and is equal to the target
If (nums nums [I] + [j]==target)
{
//the array subscript is found in the array returnSize
LST [0]=I;
LST [1]=j;
* returnSize=2;
Return LST.
//return returnSize;//don't know how to return an array {I, j}
}
//else
//{
////in order to prevent error, here need to add the else branch statement
//}
}
}
Return LST.
}
Error message:
Requirements:
Oneself use visual studio2010 debugged, may own grammar still don't have good, logic should be no problem, want to analyze,
CodePudding user response:
Int * LST=(int) malloc (sizeof (int) * 2); Instead of int * LST=(int *) malloc (sizeof (int) * 2); TryYou this is converted to an integer, the address and the integer to address