算法—两个有序数组的中位数 Median of Two Sorted Arrays
关注微信公众号:CodingTechWork,一起学习进步。
题目
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。
请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
你可以假设 nums1 和 nums2 不会同时为空。
Example 1:nums1 = [1, 3]
nums2 = [2]The median is 2.0
Example 2:nums1 = [1, 2]
nums2 = [3, 4]The median is (2 + 3)/2 = 2.5
题解
1 暴力求解
class Solution {public double findMedianSortedArrays(int[] nums1, int[] nums2) {//定义一个新的数组存放两个数组的值int[] newNums = new int[nums1.length + nums2.length];for (int i = 0; i < nums1.length; i++) {newNums[i] = nums1[i];}for (int j = 0; j < nums2.length; j++) {newNums[nums1.length + j] = nums2[j];}//排序Arrays.sort(newNums);//个位数判断,直接返回中间值if (newNums.length % 2 == 1) {return newNums[newNums.length / 2];} else { //偶数判断,返回length/2 -1及length/2和的一半return (newNums[newNums.length / 2 - 1] + newNums[newNums.length / 2]) / 2.0;}}
}
当然,时间复杂度为o(m+n)
,这是不满足题目需求的o(log(m+n))
2 二分法
class Solution {public double findMedianSortedArrays(int[] nums1, int[] nums2) {int n = nums1.length;int m = nums2.length;int left = (n + m + 1) / 2;int right = (n + m + 2) / 2;//将偶数和奇数的情况合并,如果是奇数,会求两次同样的 k 。return (getMinK(nums1, 0, n - 1, nums2, 0, m - 1, left) + getMinK(nums1, 0, n - 1, nums2, 0, m - 1, right)) * 0.5;}public static int getMinK(int[] nums1, int start1, int end1, int[] nums2, int start2, int end2, int k) {int length1 = end1 - start1 + 1;int length2 = end2 - start2 + 1;if (length1 > length2) {//短的在前,保证先为空的为length1return getMinK(nums2,start2,end2,nums1,start1,end1,k);}//说明nums1已经为空,此时返回nums2的元素if (length1 == 0) {return nums2[start2 + k -1];}if (k == 1) {return Math.min(nums1[start1], nums2[start2]);}int i = start1 + Math.min(length1, k / 2) - 1;int j = start2 + Math.min(length2, k / 2) -1;if (nums1[i] > nums2[j]) {return getMinK(nums1, start1, end1, nums2, j + 1, end2, k - (j - start2 + 1));} else {return getMinK(nums1, i + 1, end1, nums2, start2, end2, k - (i - start1 + 1));}}
}
参考 Median of Two Sorted Arrays
算法—两个有序数组的中位数 Median of Two Sorted Arrays相关推荐
- 数组越界怎么判断_算法连载之求解两个有序数组的中位数
问题 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2.找出这两个有序数组的中位数.假设 nums1 和 nums2 不会同时为空. 示例 1: nums1 = [1, 3] num ...
- 算法--------------------寻找两个有序数组的中位数
题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2.请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)).你可以假设 nums1 和 num ...
- 6 js 比较两个数组的差异_每天一道算法题(js)(3)——寻找两个有序数组的中位数...
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- python【力扣LeetCode算法题库】4- 寻找两个有序数组的中位数
寻找两个有序数组的中位数 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 n ...
- 判断给定的两个数是否是亲和数_动画演示LeetCode算法题:004-寻找两个有序数组的中位数...
题目: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nu ...
- Leetcode算法题:两个有序数组求中位数
Leetcode算法题:两个有序数组求中位数 要求时间复杂度为O(log(m+n)) 思路: 暴力解决:合并数组并排序,简单且一定能实现,时间复杂度O(m+n) 由于两个数组已经排好序,可一边排序一边 ...
- LeetCode实战:寻找两个有序数组的中位数
题目英文 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- double 数组_寻找两个有序数组的中位数
大家好,我是老皮: 题目地址:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目描述: 给定两个大小为 m 和 n 的有序数组 ...
- return两个返回值_LeetCode 第四题 寻找两个有序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
最新文章
- vscodemaven 配置_二、vscode搭建maven开发环境
- 心累了,就笑一笑,学会爱自己
- AndroidStudio自动补完包的快捷键
- 面向在线教育业务的流媒体分发演进
- SpringBoot整合oss实现文件的上传,查看,删除,下载
- Java 9 ← 2017,2019 → Java 13 ,来看看Java两年来的变化
- ZZULIOJ 1119: 一维数组排序
- 西门子smartclient怎么用_Smart Client学习体会(一) Smart Client介绍
- C#中的深度学习:Keras.NET中的硬币识别,第二部分
- 本地搭建dubbo实例
- 【图文详解】,配置NLB群集
- 人生路上好文章[收藏]
- 高性能图像放大算法——waifu2x方法
- 【机器学习】Tensorflow:理解和实现快速风格化图像fast neural style
- 无法启动计算机的病毒是,电脑病毒导致系统中的exe文件无法打开如何解决
- 天啦噜,项目上使用InputStream,我被坑了一把!
- 卐 4-3D图形的数学
- 解决CTeX工具包中MikTeX编译TeX文件报错问题
- MATLAB实战系列(十)-二维装箱问题之BL法修正版(附MATLAB代码)
- php错误测试,对 PHP 错误进行测试
热门文章
- Docker如何删除一个镜像
- linux内核那些事之Memory protection keys(硬件原理)
- OpenCV中基本数据结构(4)_Rect
- 用于故障诊断的残差收缩网络
- 165体重_大家觉得作为一个身高165的女生多少斤体重算合适?
- python字典属于无序序列_Python序列结构--字典
- 直播短视频手机APP应用下载页面静态html网页模板
- 基于 Springboot 和 Mybatis 的后台管理系统 BootDo
- 【字节网盘】emlog版收录导航主题模板
- 对应到对象 数据库驼峰_SpringJPA底层DAO查询的写法归类总结