import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums1 int整型一维数组
* @param nums2 int整型一维数组
* @return double浮点型
*/
public double findMedianSortedArrays (int[] nums1, int[] nums2) {
int index1 = 0;
int index2 = 0;
int [] combinedNums = new int[nums1.length+nums2.length];
int index = 0;
while(index1<nums1.length&&index2< nums2.length){
if(nums1[index1]>nums2[index2]){
combinedNums[index++] = nums2[index2++];
}else{
combinedNums[index++] = nums1[index1++];
}
}
while(index1<nums1.length){
combinedNums[index++] = nums1[index1++];
}
while(index2<nums2.length){
combinedNums[index++] = nums2[index2++];
}
int n = combinedNums.length;
if(n%2!=0){
return combinedNums[n/2];
}
return (combinedNums[n/2]+ combinedNums[(n-1)/2])/2.0;
}
}
本题知识点分析:
1.数组遍历
2.双指针
3.数学模拟
本题解题思路分析:
1.先将有序数组合并成一个
2.然后根据奇偶数进行返回,奇数直接返回中位数,偶数要取两个中间值然后除2.0,如果直接除2是会去掉小数的
本题使用编程语言: Java
如果你觉得本篇文章对你有帮助的话,可以点个赞支持一下,感谢~

京公网安备 11010502036488号