-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode4.java
65 lines (62 loc) · 1.7 KB
/
LeetCode4.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
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright (c) ysx. 2020-2020. All rights reserved.
*/
package com.ysx.leetcode.advanced;
/**
* @author youngbear
* @email youngbear@aliyun.com
* @date 2020/1/12 22:01
* @blog https://blog.csdn.net/next_second
* @github https://github.com/YoungBear
* @description 4. 寻找两个有序数组的中位数
* https://leetcode-cn.com/problems/median-of-two-sorted-arrays/
*/
public class LeetCode4 {
/**
* 合并两个数组,然后找到其中位数
*
* @param nums1
* @param nums2
* @return
*/
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int length1 = nums1.length;
int length2 = nums2.length;
if (0 == length1) {
return findMedianSortedArray(nums2);
} else if (0 == length2) {
return findMedianSortedArray(nums1);
}
// 合并两个数组
int[] nums = new int[length1 + length2];
int i = 0, j = 0, k = 0;
while (j < length1 && k < length2) {
if (nums1[j] < nums2[k]) {
nums[i++] = nums1[j++];
} else {
nums[i++] = nums2[k++];
}
}
while (j < length1) {
nums[i++] = nums1[j++];
}
while (k < length2) {
nums[i++] = nums2[k++];
}
return findMedianSortedArray(nums);
}
/**
* 返回单个数组的中位数
*
* @param nums
* @return
*/
private double findMedianSortedArray(int[] nums) {
int length = nums.length;
if (0 == length % 2) {
return ((double) nums[length / 2 - 1] + nums[length / 2]) / 2;
} else {
return nums[length / 2];
}
}
}