给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。

请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。

你可以假设 nums1 和 nums2 不同时为空。

示例 1:

nums1 = [1, 3] nums2 = [2] 中位数是 2.0

示例 2:

nums1 = [1, 2] nums2 = [3, 4] 中位数是 (2 + 3)/2 = 2.5

  1. bool cmp1(int x, int y)
  2. {
  3. return x < y;
  4. }
  5. class Solution {
  6. public:
  7. double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
  8. int len1 = nums1.size();
  9. int len2 = nums2.size();
  10. vector<int> v;
  11. for(int i = 0; i < len1; i++)
  12. v.push_back(nums1[i]);
  13. for(int i = 0; i < len2; i++)
  14. v.push_back(nums2[i]);
  15. sort(v.begin(), v.end(), cmp1);
  16. int len3 = v.size();
  17. if((len3 & 1) == 1)
  18. {
  19. return v[len3 / 2];
  20. }
  21. else
  22. {
  23. return (double)(v[len3 / 2 - 1] + v[len3 / 2]) / 2;
  24. }
  25. }
  26. };

Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数的更多相关文章

  1. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  2. [LeetCode] 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 ...

  3. [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 ...

  4. [LintCode] 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 sorted ...

  5. 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 ...

  6. 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. 【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 ...

  8. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. Median of Two Sorted 求两个有序数组的中位数

    中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...

随机推荐

  1. mongoDB可视化工具RoBo 3T的安装和使用

    第一步下载RoBo3T https://robomongo.org/download 第二步安装 双击安装包安装,修改安装路径,不停下一步,点击安装. 一路next,最后到了这个页面直接点击finis ...

  2. 性能压测中的SLA,你知道吗?

    本文是<Performance Test Together>(简称PTT)系列专题分享的第6期,该专题将从性能压测的设计.实现.执行.监控.问题定位和分析.应用场景等多个纬度对性能压测的全 ...

  3. 【codeforces 500D】New Year Santa Network

    [题目链接]:http://codeforces.com/problemset/problem/500/D [题意] 有n个节点构成一棵树; 让你随机地选取3个不同的点a,b,c; 然后计算dis(a ...

  4. 微信小程序注册使用流程

    新开发微信小程序使用流程:  平时使用小程序较多,但是具体注册流程简单记录下: 第一步:通过邮箱注册 第二步:在邮箱进行激活 注册后,在邮箱会收到激活信息提示.点击激活地址进行激活. 第三步:信息登记 ...

  5. 基于PtrFrameLayout实现自定义仿京东下拉刷新控件

    前言 最近基于项目需要,使用PtrFrameLayout框架实现了自定义的下拉刷新控件,大体效果类似于京东APP的下拉刷新动态效果.在这里和大家分享一下具体的思路和需要注意的地方,以便帮助有类似开发和 ...

  6. Java中String类的常见面试题

    1. 判断定义为String类型的s1和s2是否相等 String s1 = "ab"; String s2 = "abc"; String s3 = s1 + ...

  7. MySQL-Utilities:mysqldiff

    园子看到使用MySQL对比数据库表结构,参考测试发现 mysql> use test; create table test1 (id int not null primary key, a ) ...

  8. 安装springsource-tool-suite插件成功之后找不到spring的处理办法

    最近学习spring,安装springsource-tool-suite插件,成功之后,在help-installation details里面可以找到安装的spring插件,却在window-pre ...

  9. 专访阿里云资深技术专家黄省江:中国SaaS公司的成功之路

    笔者采访中国SaaS厂商10多年,深感面对获客成本巨大.产品技术与功能成熟度不足.项目经营模式难以大规模复制.客户观念有待转变等诸多挑战,很多中国SaaS公司的经营状况都不容乐观. 7月26日,阿里云 ...

  10. CSS中position和header和overflow和background

    <!DOCTYPE html> <!--CSS中position属性--> <html lang="en"> <head> < ...