关于

leetCode 取中位数

/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var findMedianSortedArrays = function (nums1, nums2) {
  let list = nums1.concat(nums2).sort((a, b) => a - b);

  let result = [];

  if (list.length === 0) {
    result = [0];
  } else if (list.length === 1) {
    result = list;
  } else if (list.length % 2 === 1) {
    const index = (list.length - 1) / 2;
    result = list.slice(index, index + 1);
  } else if (list.length % 2 === 0) {
    const index = list.length / 2;
    result = list.slice(index - 1, index + 1);
  }

  let res = result.reduce((a, b) => a + b) / result.length;

  return Number(res.toFixed(5));
};

console.log("res--->", findMedianSortedArrays([1, 3], [2]));
console.log("res--->", findMedianSortedArrays([1, 2], [3, 4]));
console.log("res--->", findMedianSortedArrays([0, 0], [0, 0]));
console.log("res--->", findMedianSortedArrays([], [1]));
console.log("res--->", findMedianSortedArrays([2], []));