Home > Back-end >  Questions about LeetCode first questions the sum of two Numbers
Questions about LeetCode first questions the sum of two Numbers

Time:12-07

Title:
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]

The code is as follows:
Nums int * twoSum (int * and an int numsSize, int target, int * returnSize) {// what the returnSize pointer variable is used for? Don't live without?
Int * res=(int *) malloc (sizeof (int) * 2);
* returnSize=0;
for(int i=0; I & lt; NumsSize - 1; I++) {
For (int j=I + 1; J & lt; NumsSize; J++) {
If (nums nums [I] + [j]==target) {
Res [0]=I;
Res [1]=j;
* returnSize=2;
return res;
}
}
}
return res;// the above already has returned to res, it returns the res reason is here?
}

CodePudding user response:

The above has been returned to res because==target, if not equal to the target did not return? Can put the back of the return as abnormal situation,
In addition, returnSize found==whether values can also be used as a target of two judgment,

Why do you write to see people thinking,

CodePudding user response:

But in addition to assigned returnSize no other operations,

CodePudding user response:

refer to the second floor weixin_45906870 response:
but no returnSize besides assigned other operations,

Yes, it is only at the time of==target assignment, this can be found through returnSize to determine if the==target,

For the return res; Return values cannot be used directly because the res in didn't find it==target, inside put random value,

CodePudding user response:

Returns the pointer to a one dimensional array, you need to specify the size of the array,
  • Related