int allLen = nums1.length + nums2.length;
int[] tmp = new int[allLen];
int i = 0,j = 0;
double result = 0.0;
int len1 = nums1.length, len2 = nums2.length;
int count = 0;
while(i < len1 && j < len2) {
if(nums1[i] < nums2[j]) {
tmp[count++] = nums1[i++];
}else if(nums2[j] < nums1[i]){
tmp[count++] = nums2[j++];
}else{
tmp[count++] = nums1[i];
tmp[count++] = nums1[i];
i++;
j++;
}
}
while(i < len1) {
tmp[count++] = nums1[i++];
}
while(j < len2) {
tmp[count++] = nums2[j++];
}
if(count % 2 == 1) {
result = tmp[count/2] *1.0;
}else {
result = (tmp[count/2-1] +tmp[count/2])*1.0 /2;
}
return result;
4. Median of Two Sorted Arrays
最新推荐文章于 2024-09-09 17:22:03 发布