[Leetcode] (Sum of two numbers in daily suppressed question 1)

Back to the original starting point, the green face in the memory
Insert picture description here

//C语言
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    
    
    if (nums == NULL || numsSize == 0) {
    
    
        *returnSize = 0;
        return NULL;
    }

    for (int i = 0; i < numsSize - 1; i++) {
    
    
        int newTarget = target - nums[i];
        for(int j = i + 1; j < numsSize; j++) {
    
    
            if(nums[j] == newTarget) {
    
    
                int *res = (int *)malloc(sizeof(int) * 2);
                res[0] = i;
                res[1] = j;
                *returnSize = 2;
                return res;
            }
        }
    }
    *returnSize = 0;
    return NULL;
}

Guess you like

Origin blog.csdn.net/qq_45657288/article/details/108912354