[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 ...
随机推荐
- Python进阶编程 面向对象
一.面向对象 1.1面向对象的基本格式 class 类名: def 方法名(self): print(123) return 123 def 方法名(self): print(123) return ...
- Executor框架(转)
摘要: Executor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相 ...
- vim学习(三)之命令
参考 Linux vi/vim vim常用命令总结
- Jade学习(五)之命令编译执行jade
首先全局安装jade,我们就可以使用jade 命令了! jade index.jade // 解析后会在文件夹中新生成一个压缩代码后的index.html 如果我们不想生成的index.html文件进 ...
- 运维dig语法
dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常 语法 1 dig(选项)(参数) 选项 1 @<服务器地址>:指定进行域名解析的域名服务器: 2 -b<ip地址&g ...
- 吴恩达机器学习103:SVM之大间隔分类器的数学原理
1.向量内积: (1)假设有u和v这两个二维向量:,接下来看一下u的转置乘以v的结果,u的转置乘以v也叫做向量u和向量v的内积,u是一个二维向量,可以将其在图上画出来,如下图所示向量u: 在横轴上它的 ...
- vue中出现 There are multiple modules with names that only differ in casing的问题
import时,文件引入的路径描述不统一,所以保留一种引入风格即可解决. 第一种,我选择统一用第一种 import GoTop from '@/components/layout/goTop' 第二种 ...
- node端口被占用
Error: listen EADDRINUSE :::8000 at Object._errnoException (util.js:992:11) at _exceptionWithHostPor ...
- 生产服务器上安装Python
2018-05-17 生产环境的服务器(以下简称内网服务器)由于安全限制,可能无法连接外网.这种情况下将无法直接使用pip命令安装python的包 一.更改pip源 - 默认pip是使用Python官 ...
- mybatis捕获sql工具
今天给大家分享一款好用的软件 https://ssrss.space/register?aff=982689 日志级别必须是debug