class Solution {//和归并排序中的”并“思想一样
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
double ans;
int i=0,j=0,k=0;
int n1=nums1.length,n2=nums2.length;
int[] nums=new int[n1+n2];
int n=nums.length;
while(i<n1 && j<n2){
if(nums1[i] <= nums2[j]) {
nums[k++] = nums1[i++];
}else{
nums[k++] = nums2[j++];
}
}
while(i < n1)
nums[k++] = nums1[i++];
while(j < n2)
nums[k++] = nums2[j++];
// for(i=0;i<n;i++)
// System.out.println(nums[i]);
if(n%2==0)
ans=((double)nums[n/2]+(double)nums[(n/2)-1])/2;
else
ans= (double)nums[n/2];
return ans;
}
}
4. 两个排序数组的中位数
最新推荐文章于 2018-07-04 20:49:36 发布