-
Notifications
You must be signed in to change notification settings - Fork 0
/
Median of Two Sorted Arrays.java
42 lines (42 loc) · 1.13 KB
/
Median of Two Sorted Arrays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2)
{
// int n=nums1.length;
// int m=nums2.length;
// int []nums3=new int[n+m];
// for(int i=0;i<n;i++)
// {
// nums3[i]=nums1[i];
// }
// for(int i=0;i<m;i++)
// {
// nums3[i+n]=nums1[i];
// }
// Arrays.sort(nums3);
// int mid=(nums3.length)/2;
// if(nums3.length%2!=0)
// return (double)nums3[mid];
// int g=nums3[nums3.length/2]+nums3[(nums3.length/2)-1];
// double x=(double)g/2;
// return x;
int len=nums1.length+nums2.length;
int[] ans=new int[len];
int j=0;
for(int i=0;i<nums1.length;i++)
{
ans[j]=nums1[i];
j++;
}
for(int i=0;i<nums2.length;i++)
{
ans[j]=nums2[i];
j++;
}
Arrays.sort(ans);
int mid=(ans.length)/2;
if(ans.length%2!=0)return (double)ans[mid];
int g=ans[ans.length/2]+ans[(ans.length/2)-1];
double x=(double)g/2;
return x;
}
}