-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedian.java
43 lines (43 loc) · 1 KB
/
median.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
43
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int n1=nums1.length;
int n2=nums2.length;
int[] res=new int[n1+n2];
int i=0,j=0,k=0;
while(i<n1 && j<n2){
if(nums1[i]<nums2[j]){
res[k]=nums1[i];
k++;
i++;
}
else if(nums2[j]<nums1[i]){
res[k]=nums2[j];
k++;
j++;
}
else{
res[k]=nums1[i];
k++;
i++;
res[k]=nums2[j];
k++;
j++;
}
}
while(i<n1){
res[k]=nums1[i];
k++;
i++;
}
while(j<n2){
res[k]=nums2[j];
k++;
j++;
}
int mid=res.length/2;
if(res.length%2==0){
return (double)(res[mid-1]+res[mid])/2;
}
return (double)res[mid];
}
}