[LeetCode]-algorithms-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)).
分析:给定两个已经排好序的数组nums1和nums2,求两个数组的中值,时间复杂度为O(log(m+n))
[]
[1]
=>1.00000
[1,1]
[1,2]
=>1.00000
[2,5,8]
[1,3,6]
=> 4.00000
[2,5,8,9]
[1,3,6,7]
=> 5.50000
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len = nums1.length + nums2.length;
int[] nums = new int[len];
int i=0;
for( ; i<nums1.length; i++)
nums[i] = nums1[i];
for(int j=0; j<nums2.length; j++,i++)
nums[i] = nums2[j];
Arrays.sort(nums);
//System.out.println(Arrays.toString(nums) + "-- len:"+len+"--len/2:"+(len/2));
return len%2==0? ((nums[len/2]+nums[len/2-1])/(2 * 1.0)):(nums[len/2]*1.0) ;
}
解:需要把两个数组合并成一个数组,并重新排序
[LeetCode]-algorithms-Median of Two Sorted Arrays的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- leetcode之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
随机推荐
- laravel框架之批刪&全選&全不選&反選
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Packet flow in l2(receive and transmit)
Receive 1. napi && none napi 讲网络收报过程,必然要涉及到网卡收报模型发展历史.总体上看,网络收报过经历了如下发展过程: 轮询 ---à 中断 ---à ...
- 模板 - 强连通分量/割点/桥 - Tarjan
int dfn[N], low[N], dfncnt, s[N], tp; int scc[N], sc; // 结点 i 所在 scc 的编号 int sz[N]; // 强连通 i 的大小 voi ...
- sql server 获取整数的函数ceiling(x)和floor(x)
--ceiling(x)返回不小于x的最小整数值,floor(x)返回不大于x的最大整数值 示例:select CEILING(-3.35), CEILING(3.35), FLOOR(-3.35), ...
- Jquery复习(九)之noConflict()
如何在页面上同时使用 jQuery 和其他框架? jQuery 和其他 JavaScript 框架 正如您已经了解到的,jQuery 使用 $ 符号作为 jQuery 的简写. 如果其他 JavaSc ...
- 国内高速下载Docker
一般情况下,我们可以从Docker官网下载docker安装文件,但是官方网站由于众所周知的原因,不是访问慢,就是下载慢.下载docker安装包动不动就要个把小时,真是极大的影响工作效率. 在这里推荐一 ...
- js事件冒泡、阻止事件冒泡以及阻止默认行为
事件冒泡 当事件发生后,这个事件就要开始传播(从里到外或者从外向里).为什么要传播呢?因为事件源本身(可能)并没有处理事件的能力,即处理事件的函数(方法)并未绑定在该事件源上.例如我们点击一个按钮时, ...
- 什么是file_sort?如何避免file_sort
阿里巴巴编码规范有这么一例 [推荐]如果有order by场景,请注意利用索引的有序性. order by最后的字段是组合索引的一部分,并且放在索引组合顺序的最后,避免出现file_sort的情况,影 ...
- iconv_close - 关闭字符转换描述符
总览 (SYNOPSIS) #include <iconv.h> int iconv_close (iconv_t cd); 描述 (DESCRIPTION) iconv_close 函数 ...
- python接口测试—mysql数据库操作
python操作mysql数据库 1.安装pymysql库 在python中安装pymysql第三方库,通过pip install pymysql 命令进行安装. 2.python操作mysql数据库 ...