-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 007-Median of Two Sorted Arrays
49 lines (45 loc) · 1.38 KB
/
Day 007-Median of Two Sorted Arrays
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
44
45
46
47
48
49
// ( --- https://leetcode.com/problems/median-of-two-sorted-arrays/ --- )
// 4. Median of Two Sorted Arrays
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length;int n = nums2.length;
int sum = m+n;
int[] a = new int[m+n];
int i=0;int j=0;int k=0;
while(i<m && j<n){
if(nums1[i]>nums2[j]){
a[k++]=nums2[j++];
}
else{
a[k++]=nums1[i++];
}
}
for(;i<m;i++){
a[k++]=nums1[i];
}
for(;j<n;j++)
{a[k++]=nums2[j];
}
if(sum%2==0){
return (a[(sum/2)-1]+a[(sum/2)])/2.0;
}
else{return a[sum/2];}
}
}
// not for if nums1 [n] > nums2[m];
// class Solution {
// public double findMedianSortedArrays(int[] nums1, int[] nums2) {
// int a = nums1.length;
// int c1 = nums1.length + nums2.length;
// int [] c = new int[c1];
// double a1 =0 ;
// if ( nums1[a-1 ] < nums2[0]){
// System.arraycopy( nums1 , 0 , c, 0, nums1.length );
// System.arraycopy( nums2 , 0 , c , nums1.length , nums2.length );
// System.out.print(Arrays.toString(c));
// if (c1 % 2 == 1)
// a1 = (c[c1/2] + c[c1/2 + 1]) / 2;
// return a1;
// }
// }
// }