-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindMedianSortedArrays.java
More file actions
28 lines (23 loc) · 959 Bytes
/
FindMedianSortedArrays.java
File metadata and controls
28 lines (23 loc) · 959 Bytes
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
package com.company;
import org.junit.Test;
public class FindMedianSortedArrays {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int i = 0, j = 0, k = 0;
int[] tmp = new int[nums1.length + nums2.length];
while (i < nums1.length && j < nums2.length) {
if (nums1[i] <= nums2[j]) tmp[k++] = nums1[i++];
else tmp[k++] = nums2[j++];
}
while (i < nums1.length) tmp[k++] = nums1[i++];
while (j < nums2.length) tmp[k++] = nums2[j++];
if ((nums1.length + nums2.length) % 2 == 1)
return tmp[(nums1.length + nums2.length) / 2];
else
return (double) (tmp[(nums1.length + nums2.length) / 2 - 1] +
tmp[(nums1.length + nums2.length) / 2]) / 2;
}
@Test
public void test() {
System.out.println(findMedianSortedArrays(new int[]{1, 3}, new int[]{2, 4}));
}
}