您的位置:首页 > 其它

Leetcode_4_寻找两个有序数组的中位数

2019-08-16 10:02 267 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/CDTZYXZZY/article/details/99672201

Leetcode —— 4 寻找两个有序数组的中位数

  • 解二:减少代码量
  • 解三:双指针排序法
  • 解三:二分查找法
  • 题目

    解一:使用数组方法

    涉及数组方法:

    • arr.splice(index,num,item);
      index:开始下表
      num:删除的元素个数
      item:要替换的元素
    • arr.sort((a,b) => a-b) 如果没有参数,默认按照元素首字母在编码表中的顺序排序
    • 在原数组上操作
    • 如果想按照其他规则排序,需要提供比较函数 比较函数有两个参数:a,b
    • 若排序后的数组中,a在b前,返回负数
    • 若与b相等,返回0
    • 若b在a前,返回正数
    • 两两比较,逐渐将元素代入到a,b中
  • 定式:
      数字由小到大排序:arr.sort((a,b)=>a-b);
    • 数字由大到小排序:arr.sort((a,b)=>b-a);
  • arr.concat(arr1):数组拼接
      将多个数组拼接为一个数组
    • arr1被拼接在arr后面
    • 返回一个新的数组,并不是在原数组上操作

    代码:

    var findMedianSortedArrays = function(nums1, nums2) {
    if(nums1.length==0&&nums2.length==0){return ;}
    let arr = nums1.concat(nums2).sort((a,b)=>a-b);
    let num = 0;
    if(arr.length%2){
    num = arr[(arr.length+1)/2-1];
    }else{
    num = (arr[arr.length/2]+arr[arr.length/2-1])/2;
    }
    
    return num;
    };

    解析:

    1. 注意参数判断是否为空
    2. 数组方法的掌握:concat(),sort()
    3. 在数组中计算中位数

    解二:减少代码量

    var findMedianSortedArrays = function(nums1, nums2) {
    const arr = [...nums1, ...nums2].sort((a, b) => a - b);
    const { length } = arr;
    return length % 2 ? arr[Math.floor(length / 2)] : (arr[length / 2] + arr[length / 2 - 1]) / 2;
    };
    
    作者:yujie-3
    链接:https://leetcode-cn.com/problems/two-sum/solution/javascript-san-chong-shi-jian-fu-za-du-by-yujie-3/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    解析:

    与上面自己写的算法差不多

    注意:
    1、 将数组合并,除了使用concat方法,还可以使用ES6中的省略符号

    ```
    let arr = [...nums1,...nums2];
    ```

    2、 使用解构赋值来对length进行赋值操作

    ```
    let {length} = arr;
    ```
    将arr的length赋给length变量
    
    ```
    let {length,pop} = arr;
    ```

    不仅可以获得数组的length属性,还可以获得数组的方法
    3、 对于只有两种情况的判断语句,可以使用三元运算符
    4、 Math对象常用方法:

    • Math.floor() : 对数进行下舍入
    • Math.ceil() : 对数进行上舍入
    • Math.abs() :返回绝对值
    • Math.random() :返回0~1的随机数
    • Math.round() :四舍五入
    • Math.pow(x,y) :返回x的y次幂

    解三:双指针排序法

    var findMedianSortedArrays = function(nums1, nums2) {
    let reIndex = nums2.length - 1;
    for (let i = nums1.length - 1; i >= 0; i--) {
    while (nums1[i] <= nums2[reIndex] && reIndex > -1) {
    nums1.splice(i + 1, 0, ...(nums2.splice(reIndex, 1)));
    reIndex--;
    }
    }
    const arr = nums2.concat(nums1);
    const { length } = arr;
    return length % 2 ? arr[Math.floor(length / 2)] : (arr[length / 2] + arr[length / 2 - 1]) / 2;
    };
    
    作者:yujie-3
    链接:https://leetcode-cn.com/problems/two-sum/solution/javascript-san-chong-shi-jian-fu-za-du-by-yujie-3/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    解三:二分查找法

    var findMedianSortedArrays = function(nums1, nums2) {
    if (nums1.length > nums2.length) [nums1, nums2] = [nums2, nums1];
    
    const length1 = nums1.length;
    const length2 = nums2.length;
    let min = 0;
    let max = length1;
    let half = Math.floor((length1 + length2 + 1) / 2);
    while (max >= min) {
    const i = Math.floor((max + min) / 2);
    const j = half - i;
    if (i > min && nums1[i - 1] > nums2[j]) {
    max = i - 1;
    } else if (i < max && nums1[i] < nums2[j - 1]) {
    min = i + 1;
    } else {
    let left,right;
    if (i === 0) left = nums2[j - 1];
    else if (j === 0) left = nums1[i - 1];
    else left = Math.max(nums1[i - 1], nums2[j - 1]);
    
    if (i === length1) right = nums2[j];
    else if (j === length2) right = nums1[i];
    else right = Math.min(nums1[i], nums2[j]);
    
    return (length1 + length2) % 2 ? left : (left + right) / 2;
    }
    }
    return 0;
    };
    
    作者:yujie-3
    链接:https://leetcode-cn.com/problems/two-sum/solution/javascript-san-chong-shi-jian-fu-za-du-by-yujie-3/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: