15. Leetcode – C language

Time:2022-5-8

1、 Double finger needling — C language implementation

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

int compare(const void *a, const void *b)
{
  return *((int *)a) - *((int *)b);
}

int **threeSum(int *nums, int numsSize, int *returnSize, int **returnColumnSizes)
{
  //If the array length is less than 3, an empty array is returned directly
  if (numsSize < 3)
  {
    *returnSize = 0;
    *returnColumnSizes = NULL;
    return NULL;
  }
  //First sort the array in ascending order, and qsort does not return any value
  qsort(nums, numsSize, sizeof(int), compare);
  *returnSize = 0;
  //Allocate storage space
  int **returnArr = (int **)malloc(sizeof(int *) * (numsSize - 2) * (numsSize - 2));
  *returnColumnSizes = (int *)malloc(sizeof(int) * (numsSize - 2) * (numsSize - 2));
  //Start the double loop and traverse the array nums
  for (int i = 0; i < numsSize; i++)
  {
    //If the element is greater than 0, the subsequent elements are also greater than 0, and the sum of the three numbers must be greater than 0
    //So you can exit the traversal directly at this time
    if (nums[i] > 0)
    {
      break;
    }
    //Remove duplicate values for outer loops
    if (i > 0 && nums[i] == nums[i - 1])
    {
      continue;
    }
    //Record the initial subscript of the right pointer
    int third = numsSize - 1;
    //Record the value corresponding to the outer loop subscript, that is, the first element of the triplet
    int oneNum = nums[i];
    for (int j = i + 1; j < numsSize; j++)
    {
      //Remove duplicate values pointed by the left pointer
      if (j > i + 1 && nums[j] == nums[j - 1])
      {
        continue;
      }
      //Remove duplicate values pointed to by the right pointer
      while (j < third && nums[j] + nums[third] > -oneNum)
      {
        third--;
      }
      //When the left and right pointers coincide, exit the inner loop
      if (j == third)
      {
        break;
      }
      //Save the triples that meet the conditions to the result array
      if (nums[j] + nums[third] == -oneNum)
      {
        returnArr[*returnSize] = (int *)malloc(sizeof(int) * 3);
        returnArr[*returnSize][0] = oneNum;
        returnArr[*returnSize][1] = nums[j];
        returnArr[*returnSize][2] = nums[third];
        (*returnColumnSizes)[*returnSize] = 3;
        *returnSize += 1;
      }
    }
  }
  return returnArr;
}
int main(void) {
  int nums[] = {-1, 0, 1, 2, -1, -4};
  int numsSize = 6;
  int returnSize;
  int *returnColumnSizes = NULL;
  int **returnArr = threeSum(nums, numsSize, &returnSize, &returnColumnSizes);
  for (int i = 0; i < returnSize; i++) {
    printf("%d %d %d\n", returnArr[i][0], returnArr[i][1], returnArr[i][2]);
  }
  return 0;
}

15. Leetcode - C language

2、 Double finger needling — JavaScript implementation

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
const threeSum = function (nums) {
  const len = nums.length
  //If the array length is less than 3, an empty array is returned directly
  if (len < 3) {
    return []
  }
  //Sort the array
  nums.sort(function (a, b) { return a - b })
  //Define an empty array as the return array
  const returnArr = []
  for (let i = 0; i < len; i++) {
    //If the element is greater than 0, the subsequent elements are also greater than 0, and the sum of the three numbers must be greater than 0
    //So you can exit the traversal directly at this time
    if (nums[i] > 0) {
      break
    }
    //Remove duplicate values for outer loops
    if (i > 0 && nums[i] === nums[i - 1]) {
      continue
    }
    //Record the initial subscript of the right pointer
    let third = len - 1
    //Record the value corresponding to the outer loop subscript, that is, the first element of the triplet
    const oneNum = nums[i]
    for (let j = i + 1; j < len; j++) {
      //Remove duplicate values pointed by the left pointer
      if (j > i + 1 && nums[j] === nums[j - 1]) {
        continue
      }
      //Remove duplicate values pointed to by the right pointer
      while (j < third && nums[j] + nums[third] > -oneNum) {
        third--
      }
      //When the left and right pointers coincide, exit the inner loop
      if (j === third) {
        break
      }
      //Push the triples that meet the conditions to the result array
      if (nums[j] + nums[third] === -oneNum) {
        returnArr.push([oneNum, nums[j], nums[third]])
      }
    }
  }
  return returnArr
}

15. Leetcode - C language

  • Time complexity: O (n ^ 2), where NN is the length of array nums.
  • Spatial complexity: O (logn). We ignore the space for storing answers, and the space complexity of additional sorting is O (logn). However, we modified the input array num, which is not necessarily allowed in practice. Therefore, it can also be regarded as using an additional array to store copies of num and sort them. The space complexity is O (n).