-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray4.java
More file actions
78 lines (77 loc) · 2.32 KB
/
Array4.java
File metadata and controls
78 lines (77 loc) · 2.32 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package array;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array4
* @Author: markey
* @Description:4. 寻找两个有序数组的中位数
* 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。
*
* 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
*
* 你可以假设 nums1 和 nums2 不会同时为空。
*
* 示例 1:
*
* nums1 = [1, 3]
* nums2 = [2]
*
* 则中位数是 2.0
* 示例 2:
*
* nums1 = [1, 2]
* nums2 = [3, 4]
*
* 则中位数是 (2 + 3)/2 = 2.5
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/4/20 22:39
* @Version: 1.0
*/
public class Array4 {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int midNum = (nums1.length + nums2.length) / 2;
int num = 0;
int nums1Index = 0, num2Index = 0;
for (int i = 0; i < midNum; i++) {
if (nums1Index >= nums1.length) {
num = nums2[num2Index];
num2Index++;
continue;
}
if (num2Index >= nums2.length) {
num = nums1[nums1Index];
nums1Index++;
continue;
}
if (nums1[nums1Index] < nums2[num2Index]) {
num = nums1[nums1Index];
nums1Index++;
} else {
num = nums2[num2Index];
num2Index++;
}
}
double res;
if ((nums1.length + nums2.length) % 2 == 0) {
if (nums1Index >= nums1.length) {
res = (num + nums2[num2Index]) / 2;
} else if (num2Index >= nums2.length) {
res = (num + nums1[nums1Index]) / 2;
} else {
res = (num + Math.min(nums1[nums1Index], nums2[num2Index])) / 2.0;
}
} else {
if (nums1Index >= nums1.length) {
res = nums2[num2Index];
} else if (num2Index >= nums2.length) {
res = nums1[nums1Index];
} else {
res = Math.min(nums1[nums1Index], nums2[num2Index]);
}
}
return res;
}
}