LeetCode 4. Median of Two Sorted Arrays
题目:
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)).
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
思路:
就是求两个数组合并后的数组的中位数,利用归并思想合并,再求中位数,不难。
计算nums1和nums2的长度len1、len2,记len为len1和len2的和,如果和都0,返回double类型的0。利用归并思想,如果i小于len1,j小于len2,将nums1[i]和nums2[j]中小的那个值放入res,并对应索引加1。上面循环结束,如果是i
class Solution {
public:double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {int len1=nums1.size();//计算nums1和nums2的长度int len2=nums2.size();int len=len1+len2;//记len为len1和len2的和if(len==0){//如果和都0,返回double类型的0return double(0);}vector<int> res;int i=0,j=0;//利用归并思想while(i<len1&&j<len2){//如果i小于len1,j小于len2res.push_back(nums1[i]<nums2[j]?nums1[i++]:nums2[j++]);//将nums1[i]和nums2[j]中小的那个值放入res,并对应索引加1}while(i<len1){//上面循环结束,如果是i<len1,也就是j已经到达len2,将nums1中剩余的数放入resres.push_back(nums1[i++]);}while(j<len2){//上面循环结束,如果是j<len2,也就是i已经到达len1,将nums2中剩余的数放入resres.push_back(nums2[j++]);}if((len1+len2)%2==0){//如果总个数为偶数个,则中位数为索引值为len/2与len/2-1的两个值转换为double类型后的平均值return ((double(res[len/2])+double(res[len/2-1]))/2);}else{//如果总个数为奇数,中位数为索引值为len/2的那个值转换为double类型后的值return double(res[len/2]);}}
};
输出结果: 52ms
LeetCode 4. Median of Two Sorted Arrays相关推荐
- 【leetcode】Median of Two Sorted Arrays
题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...
- LeetCode 4 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- LeetCode.004 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- leetcode - 4. Median of Two Sorted Arrays
Degree Easy ★ (*´╰╯`๓)♬ Description: There are two sorted arrays nums1 and nums2 of size m and n res ...
- LeetCode | 4. Median of Two Sorted Arrays(中位数)
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- java打乱一组正序数字,Leetcode︱4.Median of Two Sorted Arrays寻找两个正序数组的中位数.java...
题目 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 示例 : 输入:nums1 = [1,3], nums2 = [2 ...
- leetcode 之Median of Two Sorted Arrays(五)
找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值. 参考下面的思路,非常的清晰: 代码: double findMedianofTwoSortArrays(int A[], int B[ ...
- 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数
题目:LeetCode 004 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m an ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...
最新文章
- 数据中设计中的范式与反范式
- Phpstorm 9 关闭拼写检查
- 手机端网站排名优化需注意哪些细节?
- grenndao 插入表数据报错
- 【H.264/AVC视频编解码技术】第三章【熵编码】
- 【连载】如何掌握openGauss数据库核心技术?秘诀三:拿捏存储技术(4)
- LINUX:Contos7.0 / 7.2 LAMP+R 下载安装Php篇
- OpenCV中感兴趣区域的选取与检测(一)
- 河南省第九届ACM程序设计大赛总结
- C#编写的winform程序绑定comboBox成功,添加一个默认的文字选项请选择
- 华为MA5300配置RADIUS认证
- 给JAVA初学者的建议(转载治phphot的一个牛人给java初学者的建议)
- python 累加直方图_二维数组的Python累积直方图
- C++——迪杰斯特拉算法弗洛伊德算法(DijkstraFloyd)for Neuedu
- java春招面试冲刺系列:mysql基础知识超详细复习
- 数据结构梳理(6) - 图
- 互联网故事:从瞧不上到暗暗惊讶,一套商城竞拍系统的开发设计之路!
- 信息系统管理项目监理工作:四控三管一协调
- Linux操作系统基础知识(适用于Centos/Ubuntu)
- Python 之自动获取公网IP
热门文章
- simplexml php,PHP 使用 SimpleXML 遇到冒号「:」的解法
- 一维卷积神经网络、卷积神经网络的基础知识
- Linux 16.04 右上角输入法丢失的问题
- docker公共存储库_Docker入门(2)——镜像结构和私有镜像库
- 语音技术(百度语音)开发 - 第一篇
- html外边框设为虚线,科技常识:html设置虚线边框的方法
- 软著文档鉴别材料_软著申请被驳回补正材料期限是多久?逾期未补正申请被撤回怎么办?...
- delphi 解析一维条码_一维码和二维码区别有哪些
- ImovieBox视频下载工具
- linux 网络瘫痪,Linux内核发现TCP漏洞,小流量也能DoS瘫痪设备