[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 ...
随机推荐
- 实例学习——爬取Pexels高清图片
近来学习爬取Pexels图片时,发现书上代码会抛出ConnectionError,经查阅资料知,可能是向网页申请过于频繁被禁,可使用time.sleep(),减缓爬取速度,但考虑到爬取数据较多,运行时 ...
- 使用Servlet实现验证码
没有验证码带来的问题 对特定用户不断登录破解密码. 对某个网站创建账户. 对某个网站提交垃圾数据. 对某个网站刷票. 通过验证码由用户肉眼识别其中的验证码信息,从而区分用户是人还是计算机. 定义: ...
- 如何申请百度小程序的appid(目前不支持个人账号申请)
一.搜索百度智能小程序,并使用百度账号登陆 填写相关资料进入审核阶段,审核成功即可进入百度小程序开发者后台.打开“智能小程序首页”-“设置”-“开发设置”, 查看百度小程序的 AppID
- spring-cloud学习BUG
1.com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: c ...
- gradle 刷新打包的时候报错
java.lang.AbstractMethodError: org.jetbrains.plugins.gradle.tooling.util.ModuleComponentIdentifierIm ...
- Maven项目构建利器02——安装Maven核心程序
1.Maven也是用Java编写的工具,同样运行在JVM上,所以我们在安装Maven之前需要确保已经安装了JDK,首先来检查电脑上是否安装JDK. 2.如上图说明电脑中已经安装了JDK,下面就要去官网 ...
- jumpserver模块功能介绍
一.仪表盘二.用户管理1.用户列表2.用户组 三.资产管理 1.资产列表 1.1 管理资产树 资产树节点不能重名, 右击节点可以添加.删除和重命名节点, 以及进行资产相关的操作 1.2 为资产树节点创 ...
- linux centos中安装flash player
本机为centos 7.0 64 1.用火狐浏览器随便打开一个视频网站,找到一个视频点进去,网站会提示未安装flash player.点击进去到adobe官网.2.下载flash player插件,可 ...
- Azure云服务托管恶意软件
微软Azure云服务被用于托管恶意软件,可控制多达90台电脑 BleepingComputer称,在早期报道中,5月份陆续出现了两起与Azure相关的恶意软件攻击事件: 1.自5月10日以来,Azur ...
- SpringBootMVC01——A simple SpringBootMVC Sample
不带数据库的SpringBootMVC案例 1.创建一个SpringBoot项目,添加thymeleaf,webstarter 2.目录层级 3.启动器代码 package com.littlepag ...