class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] num=mergeArrays(nums1,nums2);
int length=num.length;
int mid=length>>1;
double result=0;
if (length%2!=0){
result=num[mid];
}else {
double i1=num[mid-1];
double i2=num[mid];
result=(i1+i2)/2;
}
return result;
}
public int[] mergeArrays(int[] nums1,int[] nums2){
int length_nums1=nums1.length;
int length_nums2=nums2.length;
int length=length_nums1+length_nums2;
int[] num=new int[length];
int p1=0,p2=0;
for(int i=0;i<length;i++){
if (p1<length_nums1&&p2<length_nums2){
if (nums1[p1]<=nums2[p2]){
num[i]=nums1[p1];
p1++;
}else {
num[i]=nums2[p2];
p2++;
}
}else {
while (p1<length_nums1){
num[i]=nums1[p1];
i++;
p1++;
}
while (p2<length_nums2){
num[i]=nums2[p2];
i++;
p2++;
}
}
}
return num;
}
}
思路是合并数组,再从中找中位数。