Leetcode_4_寻找两个有序数组的中位数
2019-08-16 10:02
267 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/CDTZYXZZY/article/details/99672201
解二:减少代码量 解三:双指针排序法 解三:二分查找法
定式:
数字由大到小排序:arr.sort((a,b)=>b-a);
arr.concat(arr1):数组拼接
arr1被拼接在arr后面
返回一个新的数组,并不是在原数组上操作
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);
-
将多个数组拼接为一个数组
代码:
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; };
解析:
- 注意参数判断是否为空
- 数组方法的掌握:concat(),sort()
- 在数组中计算中位数
解二:减少代码量
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) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章推荐
- leetcode第四题:寻找两个有序数组的中位数
- [leetcode 4] 寻找两个有序数组的中位数 python
- LeetCode4 寻找两个有序数组的中位数 Median of Two Sorted Arrays
- 【LeetCode】第四题:寻找两个有序数组的中位数
- 基础算法之寻找两个有序数组的中位数(LeetCode-4)
- LeetCode 寻找两个有序数组的中位数
- leetcode 寻找两个有序数组的中位数
- leetcode之数组类之数组的旋转与分治类-----OJ 189/33/81/153/154 数组旋转 旋转数组搜索 88 有序数组合并 4 两个有序数组寻找第K个元素/中位数 35 寻找插入位置
- leetcode 寻找两个有序数组的中位数 median of two sorted arrays
- leetcode 4. 寻找两个有序数组的中位数(Java版)
- Leetcode:4.寻找两个有序数组的中位数
- leetcode寻找两个有序数组的中位数C++版
- [LeetCode]4. 寻找两个有序数组的中位数/C++
- LeetCode第4题--寻找两个有序数组的中位数
- 寻找两个有序数组的中位数(LeetCode)
- [leetcode] Median of Two Sorted Arrays 寻找两个有序数组的中位数
- LeetCode4.python实现:寻找两个有序数组中的中位数问题☆☆☆
- leetcode4 寻找两个有序数组的中位数 python
- LeetCode刷题笔记(Java版)----4. 寻找两个有序数组的中位数
- LeetCode题解(python)-4. 寻找两个有序数组的中位数