C solution (3ms)


  • 0
    int* twoSum(int* nums, int numsSize, int target, int* resultSize) {
        *resultSize = 0;
        if (!nums || numsSize < 1) return NULL;
        int i = 0, j = numsSize - 1;
        while (i < j) {
            int sum = nums[i] + nums[j];
            if (sum > target) {
                j--;
            } else if (sum < target) {
                i++;
            } else {
                *resultSize = 2;
                int *result = malloc(*resultSize * sizeof(int));
                result[0] = i + 1;
                result[1] = j + 1;
                return result;
            }
        }
        return NULL;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.