解题思路
先把两个数组组合成一个数组,然后将其进行快速排序。
中位数就是数组最中间的一个或两个,直接求即可。
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int length = nums1.length + nums2.length;
int[] nums = new int[length];
for(int i = 0;i < length;i++){
if(i < nums1.length){
nums[i] = nums1[i];
}
else {
nums[i] = nums2[i - nums1.length];
}
}
Arrays.sort(nums);
if(length%2 == 1){
return 1.0*nums[length/2];
}
else {
return (nums[length/2-1] + nums[length/2])/2.0;
}
}
}