[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 ...
随机推荐
- 3424:Candies(差分约束,Dijkstra)(配对堆优化
题面链接 题解 令x-y<=z表示x最大比y大z. 若b-a<=k1, c-b<=k2, c-a<=k3,那么c-a最大为多少呢?显然应该等于min(k1+k2, k3).可以 ...
- [.net core]3. Project 文件简介
这是一个C#的空的.net core web app .net frame work的.csproj 要编辑的话,得先卸载项目, .net core的.csproje不必要, .csproj 的文件 ...
- .net 分布式锁
原文 : 浅解.Net分布式锁的实现 序言 我晚上有在公司多呆会儿的习惯,所以很多晚上我都是最后一个离开公司的.当然也有一些同事,跟我一样喜欢在公司多搞会儿.这篇文章就要从,去年年末一个多搞会的晚 ...
- 一、JS基本基础
一.主流编辑器 早期 atom 前几年sublime 小巧,轻量,功能插件较多: webstorm 集成开发环境 vscode 免费开源的. 运行环境 : 浏览器端 谷歌,IE,fi ...
- nginx的rewrite
nginx服务的rewrite nginx后端服务的指令 1)upstream指令 :设置后端服务器组的主要指令 Upstream name {} 2)server指令:用于设定组内的服务器 3)Ip ...
- mysql排错小指南
mysql排错小指南 查询运行很慢时,可以执行mysql> show processlist\G mysql> show processlist\G ******************* ...
- VM错误解决:This Virtual Machine Appears To Be In Use
刚才准备做网站(数据备份都在VM里面),没想到启动不起来,咋一看,出现This Virtual Machine Appears To Be In Use字号,不过貌似我没有启动任何VM啊,何来in u ...
- jmeter之Ramp-up Period(in seconds)
[1]决定多长时间启动所有线程.如果使用10个线程,ramp-up period是100秒,那么JMeter用100秒使所有10个线程启动并运行.每个线程会在上一个线程启动后10秒(100/10)启动 ...
- docker 部署springboot项目【转载】
https://www.cnblogs.com/ityouknow/p/8599093.html
- c++列举出本地打印机和网络打印机名称
主要使用EnumPrinters函数 该函数枚举可用的打印机,打印服务器,域或印刷服务供应商. 代码:(开箱即用) #include <Windows.h> #include <st ...