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 sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
详见:https://leetcode.com/problems/median-of-two-sorted-arrays/description/
Java实现:
方法一:常规解法
import java.util.Arrays;
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int size1=nums1.length;
int size2=nums2.length;
int size=size1+size2;
int[] nums = new int[size];
for(int i = 0; i<size1; i++){
nums[i] = nums1[i];
}
for(int i = size1; i<size; i++){
nums[i] = nums2[i-size1];
}
Arrays.sort(nums);
double median;
if((size-1)%2 == 0){
median = nums[(size-1)/2] * 1.0;
} else {
median = (nums[(size-1)/2] + nums[((size-1)/2)+1])/2.0;
}
return median;
}
}
方法二:求第k小的数
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int size1 = nums1.length;
int size2 = nums2.length;
int size = size1 + size2;
if(size % 2 == 1){
return findKth(nums1, 0, size1, nums2, 0, size2, size / 2 + 1);
}else{
return (findKth(nums1, 0, size1, nums2, 0, size2, size / 2) + findKth(nums1, 0, size1, nums2, 0, size2, size / 2 + 1)) /2;
}
}
public double findKth(int[] nums1, int start1, int size1, int[] nums2, int start2, int size2, int k){
if(size1 - start1 > size2 -start2){
return findKth(nums2, start2, size2, nums1, start1, size1, k);
}
if(size1 - start1 == 0){
return nums2[k - 1];
}
if(k == 1){
return Math.min(nums1[start1], nums2[start2]); // k==1表示已经找到第k-1小的数,下一个数为两个数组start开始的最小值
}
int p1 = start1 + Math.min(size1 - start1, k / 2); // p1和p2记录当前需要比较的那个位
int p2 = start2 + k - p1 + start1;
if(nums1[p1 - 1] < nums2[p2 - 1]){
return findKth(nums1, p1, size1, nums2, start2, size2, k - p1 + start1);
}else if(nums1[p1 - 1] > nums2[p2 -1]){
return findKth(nums1, start1, size1, nums2, p2, size2, k - p2 + start2);
}else{
return nums1[p1 - 1];
}
}
}
C++实现:
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int size1=nums1.size();
int size2=nums2.size();
int size=size1+size2;
if(size&0x1)
return findKTh(nums1.begin(),size1,nums2.begin(),size2,size/+);
else
return (findKTh(nums1.begin(),size1,nums2.begin(),size2,size/)+findKTh(nums1.begin(),size1,nums2.begin(),size2,size/+))/;
}
double findKTh(vector<int>::iterator nums1,int m,vector<int>::iterator nums2,int n,int k)
{
if(m>n)
return findKTh(nums2,n,nums1,m,k);
if(m==)
return *(nums2+k-);
if(k==)
return min(*nums1,*nums2);
int pa=min(k/,m),pb=k-pa;
if(*(nums1+pa-)<*(nums2+pb-))
return findKTh(nums1+pa,m-pa,nums2,n,k-pa);
else if(*(nums1+pa-)>*(nums2+pb-))
return findKTh(nums1,m,nums2+pb,n-pb,k-pb);
else
return *(nums1+pa-);
}
};
参考:
https://www.cnblogs.com/leavescy/p/5877627.html
https://www.cnblogs.com/bakari/p/5082155.html
http://blog.csdn.net/yutianzuijin/article/details/11499917/
004 Median of Two Sorted Arrays 两个有序数组的中位数的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- 【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 ...
- 【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 ...
- 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 ...
- Median of Two Sorted 求两个有序数组的中位数
中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...
随机推荐
- cmake opencv,dlib 编译静态库 1
无论windows,linux 所有的库 ,dlib,opencv 通过cmake-gui 设置好静态库, 动态库,和其他各个选项 Tips: cmake 优先级用cmake-gui,因为命令太多,容 ...
- Skyline实现橡皮筋效果绘制矩形框
这种类似于框选的效果用的比较普遍,一般三维平台和GIS平台都提供了支持接口,可是Skyline就是这么傲娇! 思路是这样的:绘制出的矩形框应该是一直与屏幕边框平行的,也就是矩形框的实际旋转角度是等于摄 ...
- 使用变参函数实现pwd命令
#include "stdafx.h"#include <Windows.h> #define DIRNAME_LEN (MAX_PATH+2) BOOL PrintS ...
- kvm ip查看
1.virsh --list(查看有哪些服务器) 2.virsh dumpxml 虚拟机名称 查看服务器对应的mac地址 3.然后再宿主机上arp -a 查看对应的mac地址对应的ip
- python 基础 字典生成式
dict1 = {1:2,3:4,6:7,9:10} print dict((v,k) for k,v in dict.items()) 结果 {2:1.4:3,10:9,7:6} res = [{' ...
- Ruby环境搭建与“Hello World”
Ruby的环境搭建比较简单,在http://rubyinstaller.org/downloads/可以得到Ruby的安装包, 安装过程没什么问题.安装完成之后需要配置一下环境变量: 在PATH中填入 ...
- net.sf.json-lib maven依赖问题.
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</art ...
- shell批量创建文件及改名
批量创建文件及改名企业面试题2:使用for循环在/usr/sunzy目录下通过随机小写10个字母,批量创建10个html文件. #!/bin/bash Path=/usr/sunzy [ -d $Pa ...
- 24、sam- 详解
http://note.youdao.com/share/?id=312fa04209cb87f7674de9a9544f329a&type=note#/ https://davetang.o ...
- HDU 5245 Joyful (期望)
题意:进行K次染色,每次染色会随机选取一个以(x1,y1),(x2,y2)为一组对角的子矩阵进行染色,求K次染色后染色面积的期望值(四舍五入). 析:我们可以先求出每个格子的期望,然后再加起来即可.我 ...