Leetcode1 两数之和

 下面为正确通过的C语言版本:

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

希望对你有所帮助,谢谢!

你可能感兴趣的:(算法)