-
Notifications
You must be signed in to change notification settings - Fork 0
/
MedianofTwoSortedArrays.java
54 lines (46 loc) · 1.44 KB
/
MedianofTwoSortedArrays.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
44
45
46
47
48
49
50
51
52
53
54
package arrays;
import utils.LeetCode;
import utils.Level;
@LeetCode(no=4,
level = Level.HARD,
url = "https://leetcode.com/problems/median-of-two-sorted-arrays/",
title = "Median of Two Sorted Arrays")
public class MedianofTwoSortedArrays {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int n1 = nums1.length;
int n2 = nums2.length;
if (n1 == 1 & n2 == 1) {
return (double) (nums1[0] + nums2[0]) / 2;
}
int result[] = new int[n1 + n2];
int i = 0;
int j = 0;
for (int k = 0; k < result.length; k++) {
if (i < n1 && j < n2) {
if (nums1[i] < nums2[j]) {
result[k] = nums1[i];
i++;
} else if (nums1[i] > nums2[j]) {
result[k] = nums2[j];
j++;
} else {
result[k] = nums1[i];
result[++k] = nums2[j];
i++;
j++;
}
} else if (i > n1 - 1) {
result[k] = nums2[j];
j++;
} else if (j > n2 - 1) {
result[k] = nums1[i];
i++;
}
}
int n = n1 + n2;
if (n % 2 == 0) {
return (double) (result[n / 2] + result[(n / 2) - 1]) / 2;
} else
return (double) result[n / 2];
}
}