16. The closest is leetcode — C language and JS

Time:2022-5-6

1、 Double finger needling — C language implementation

/**
 *The nearest sum of three numbers 
 *Leetcode question 16
 * author: aliao   language: C
*/

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

int compare (const void *a, const void *b) 
{
  return (*(int *)a) - (*(int *)b);
}
int threeSumClosest(int *nums, int numsSize, int target)
{
  //Sort the array in ascending order first
  qsort(nums, numsSize, sizeof(int), compare); 
  //Define return value
  int best;
  for (int i = 0; i < numsSize; i++)
  {
    if (i > 0 && nums[i] == nums[i - 1]) {
      continue;
    }
    int head = i + 1;
    int tail = numsSize - 1;
    while (head < tail)
    {
      //Sum
      int sum = nums[i] + nums[head] + nums[tail];
      //Save the sum of the first three numbers as the closest solution best
      if (i == 0 && head == 1 && tail == numsSize - 1)
      {
        best = sum;
      }
      //Update the sum of three elements
      if (abs(sum - target) < abs(best - target)) {
        best = sum; 
      }
      if (sum < target) {
        //Left pointer moves left
        head++;
        //Left pointer de duplication
        while (nums[head] == nums[head - 1])
        {
          head++;
        }
      } else if (sum > target) {
        //Right pointer moves left
        tail--;
        //Right pointer de duplication
        while (nums[tail] == nums[tail + 1])
        {
          tail--;
        }
      } else {
        return target;
      }
    }
  }
  return best;
}

int main (void)  {
  int nums[] = {-1, 2, 1, -4};
  int numsSize = 4;
  int target = 1;
  printf("%d\n", threeSumClosest(nums, numsSize, target));
}

16. The closest is leetcode -- C language and JS

2、 Double finger needling — JS implementation

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
const threeSumClosest = function (nums, target) {
  const len = nums.length
  nums.sort((a, b) => { return a - b })
  let best = Infinity
  for (let i = 0; i < len; i++) {
    //Remove the repeated elements of the first layer cycle
    if (i > 0 && nums[i] === nums[i - 1]) {
      continue
    }
    let tail = len - 1
    let head = i + 1
    while (head < tail) {
      //Sum
      const sum = nums[i] + nums[head] + nums[tail]
      //Update the sum of three elements
      if (Math.abs(sum - target) < Math.abs(best - target)) {
        best = sum
      }
      if (sum > target) {
        //Right pointer moves left 1
        tail--
        //Left pointer de duplication
        while (nums[tail] === nums[tail + 1]) {
          tail--
        }
      } else if (sum < target) {
        //Shift left pointer to right
        head++
        //Right pointer de duplication
        while (nums[head] === nums[head - 1]) {
          head++
        }
      } else {
        //When sum is equal to target, target is returned directly
        return target
      }
    }
  }
  return best
}
console.log(threeSumClosest([-1, 2, 1, -4], 1))

16. The closest is leetcode -- C language and JS

  • Time complexity: O (n ^ 2), where n is the length of array nums. First, we need o (nlogn) to sort the array. Then, in the process of enumeration, we use one loop o (n) to enumerate a and double pointer o (n) to enumerate B and C, so there is O (n ^ 2).
  • Space complexity: O (logn). Sorting requires o (logn) space. However, we modified the input array num, which is not necessarily allowed in practice. Therefore, the worst case is to use an additional array to store copies of num and sort them. At this time, the space complexity of using the array is O (n). The sorting space complexity is O (logn), and the additional new array space complexity is O (n), so it is O (logn).