-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2.java
More file actions
48 lines (39 loc) · 1.09 KB
/
Solution2.java
File metadata and controls
48 lines (39 loc) · 1.09 KB
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
/**
* code by : 老王
* https://github.com/simplemain
*/
import java.util.Arrays;
public class Solution2
{
public static void main(String[] args)
{
final int[] nums1 = {1, 3};
final int[] nums2 = {2,4};
final Solution sol = new Solution2().new Solution();
final double result = sol.findMedianSortedArrays(nums1, nums2);
System.out.printf("%g\n", result);
}
class Solution
{
public double findMedianSortedArrays(int[] nums1, int[] nums2)
{
final int totalLength = nums1.length + nums2.length;
final int[] all = new int[totalLength];
int position = 0, p1 = 0, p2 = 0;
while (p1 < nums1.length && p2 < nums2.length)
{
all[position++] = nums1[p1] <= nums2[p2] ? nums1[p1++] : nums2[p2++];
}
if (p1 < nums1.length)
{
for (int i = p1; i < nums1.length; i++) all[position++] = nums1[i];
}
else
{
for (int i = p2; i < nums2.length; i++) all[position++] = nums2[i];
}
// 把奇数和偶数情况合并. 奇数情况下, (totalLength - 1) / 2 == (totalLength) / 2
return 0.5 * (all[(totalLength - 1) / 2] + all[(totalLength) / 2]);
}
}
}