There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3] nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
public class Solution
{
public double findMedianSortedArrays(int[] nums1, int[] nums2)
{
List<Integer> list = new LinkedList<Integer>();
for (int i = 0; i <nums1.length; i++) {
list.add(nums1[i]);
}
for (int i = 0; i <nums2.length; i++) {
list.add(nums2[i]);
}
Collections.sort(list);
if(list.size()%2==0)
{
double result=list.get(list.size()/2)+list.get(list.size()/2-1);
return result/2;
}
else
{
return list.get((int)list.size()/2);
}
}
}