1
- ## 题目地址
1
+ ## 题目地址(4. 寻找两个正序数组的中位数)
2
2
https://leetcode.com/problems/median-of-two-sorted-arrays/
3
3
4
4
## 题目描述
5
5
```
6
- There are two sorted arrays nums1 and nums2 of size m and n respectively.
6
+ 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。
7
7
8
- Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+ n)).
8
+ 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
9
9
10
- You may assume nums1 and nums2 cannot be both empty.
10
+ 你可以假设 nums1 和 nums2 不会同时为空。
11
11
12
- Example 1:
12
+
13
+
14
+ 示例 1:
13
15
14
16
nums1 = [1, 3]
15
17
nums2 = [2]
16
18
17
- The median is 2.0
18
- Example 2:
19
+ 则中位数是 2.0
20
+ 示例 2:
19
21
20
22
nums1 = [1, 2]
21
23
nums2 = [3, 4]
22
24
23
- The median is (2 + 3)/2 = 2.5
25
+ 则中位数是 (2 + 3)/2 = 2.5
26
+
24
27
```
25
28
26
29
## 思路
27
30
首先了解一下Median的概念,一个数组中median就是把数组分成左右等分的中位数。
28
31
29
32
如下图:
30
- ![ median] ( ../assets/problems/4.median-of-two-sorted-array-1.jpg )
33
+ ![ image.png] ( https://pic.leetcode-cn.com/100b34b378d0667969e7a4ca537c74e5103ce302731796740b3fa62b8bc55629-image.png )
34
+
31
35
32
36
这道题,很容易想到暴力解法,时间复杂度和空间复杂度都是` O(m+n) ` , 不符合题中给出` O(log(m+n)) ` 时间复杂度的要求。
33
37
我们可以从简单的解法入手,试了一下,暴力解法也是可以被Leetcode Accept的. 分析中会给出两种解法,暴力求解和二分解法。
@@ -44,7 +48,8 @@ The median is (2 + 3)/2 = 2.5
44
48
5 . 如果` i ` 移动到` A ` 数组最后,那么直接把剩下的所有` B ` 依次放入新的数组中.
45
49
46
50
Merge的过程如下图。
47
- ![ merge two sorted array] ( ../assets/problems/4.median-of-two-sorted-array-2.jpg )
51
+ ![ image.png] ( https://pic.leetcode-cn.com/966c9a0fea7a5f433b82d660f82c5d8184a2ac73b8362d7be435aa0f63377a4c-image.png )
52
+
48
53
49
54
50
55
* 时间复杂度: ` O(m+n) - m is length of A, n is length of B ` *
@@ -60,13 +65,14 @@ Merge的过程如下图。
60
65
对数组A的做partition的位置是区间` [0,m] `
61
66
62
67
如图:
63
- ![ partition A,B ] ( ../assets/problems/4.median-of-two-sorted-array-3 .png)
68
+ ![ image.png ] ( https://pic.leetcode-cn.com/816717da264c9e6970bfe0d696d9076febfe04b819f80a224cf2c73845d0f161-image .png)
64
69
65
70
下图给出几种不同情况的例子(注意但左边或者右边没有元素的时候,左边用` INF_MIN ` ,右边用` INF_MAX ` 表示左右的元素:
66
- ![ median examples ] ( ../assets/problems/4.median-of-two-sorted-array-5 .png)
71
+ ![ image.png ] ( https://pic.leetcode-cn.com/fe03bdcf4db4d74a0bebaa2907df3038c080e5915932fd581d06d4eb930b3035-image .png)
67
72
68
73
下图给出具体做的partition 解题的例子步骤,
69
- ![ median partition example] ( ../assets/problems/4.median-of-two-sorted-array-4.png )
74
+ ![ image.png] ( https://pic.leetcode-cn.com/1c2093328c4edf06e416d0f43a94ed42b5a46ecc9f7ed72004b40b9fb47e12a4-image.png )
75
+
70
76
71
77
* 时间复杂度: ` O(log(min(m, n)) - m is length of A, n is length of B ` *
72
78
@@ -88,9 +94,17 @@ median = max(maxLeftA, maxLeftB)
88
94
median = (max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2
89
95
```
90
96
91
- ## 代码(Java code)
97
+ ## 代码
98
+
99
+
100
+ 代码支持: Java,JS:
101
+
102
+
103
+ Java Code:
104
+
92
105
* 解法一 - 暴力解法(Brute force)*
93
- ``` java
106
+
107
+ ``` java []
94
108
class MedianTwoSortedArrayBruteForce {
95
109
public double findMedianSortedArrays (int [] nums1 , int [] nums2 ) {
96
110
int [] newArr = mergeTwoSortedArray(nums1, nums2);
@@ -127,54 +141,7 @@ class MedianTwoSortedArrayBruteForce {
127
141
}
128
142
}
129
143
```
130
- * 解法二 - 二分查找(Binary Search)*
131
- ``` java
132
- class MedianSortedTwoArrayBinarySearch {
133
- public static double findMedianSortedArraysBinarySearch (int [] nums1 , int [] nums2 ) {
134
- // do binary search for shorter length array, make sure time complexity log(min(m,n)).
135
- if (nums1. length > nums2. length) {
136
- return findMedianSortedArraysBinarySearch(nums2, nums1);
137
- }
138
- int m = nums1. length;
139
- int n = nums2. length;
140
- int lo = 0 ;
141
- int hi = m;
142
- while (lo <= hi) {
143
- // partition A position i
144
- int i = lo + (hi - lo) / 2 ;
145
- // partition B position j
146
- int j = (m + n + 1 ) / 2 - i;
147
-
148
- int maxLeftA = i == 0 ? Integer . MIN_VALUE : nums1[i - 1 ];
149
- int minRightA = i == m ? Integer . MAX_VALUE : nums1[i];
150
-
151
- int maxLeftB = j == 0 ? Integer . MIN_VALUE : nums2[j - 1 ];
152
- int minRightB = j == n ? Integer . MAX_VALUE : nums2[j];
153
-
154
- if (maxLeftA <= minRightB && maxLeftB <= minRightA) {
155
- // total length is even
156
- if ((m + n) % 2 == 0 ) {
157
- return (double ) (Math . max(maxLeftA, maxLeftB) + Math . min(minRightA, minRightB)) / 2 ;
158
- } else {
159
- // total length is odd
160
- return (double ) Math . max(maxLeftA, maxLeftB);
161
- }
162
- } else if (maxLeftA > minRightB) {
163
- // binary search left half
164
- hi = i - 1 ;
165
- } else {
166
- // binary search right half
167
- lo = i + 1 ;
168
- }
169
- }
170
- return 0.0 ;
171
- }
172
- }
173
- ```
174
-
175
- ## 代码 (javascript code)
176
- * 解法一 - 暴力解法(Brute force)*
177
- ``` js
144
+ ``` javascript []
178
145
/**
179
146
* @param {number[]} nums1
180
147
* @param {number[]} nums2
@@ -206,8 +173,12 @@ var findMedianSortedArrays = function(nums1, nums2) {
206
173
};
207
174
```
208
175
176
+ *** 复杂度分析***
177
+ - 时间复杂度:$O(max(m, n))$
178
+ - 空间复杂度:$O(m + n)$
179
+
209
180
* 解法二 - 二分查找(Binary Search)*
210
- ``` js
181
+ ``` js []
211
182
/**
212
183
* 二分解法
213
184
* @param {number[]} nums1
@@ -243,4 +214,57 @@ var findMedianSortedArrays = function(nums1, nums2) {
243
214
}
244
215
}
245
216
};
246
- ```
217
+ ```
218
+ ``` java []
219
+ class MedianSortedTwoArrayBinarySearch {
220
+ public static double findMedianSortedArraysBinarySearch (int [] nums1 , int [] nums2 ) {
221
+ // do binary search for shorter length array, make sure time complexity log(min(m,n)).
222
+ if (nums1. length > nums2. length) {
223
+ return findMedianSortedArraysBinarySearch(nums2, nums1);
224
+ }
225
+ int m = nums1. length;
226
+ int n = nums2. length;
227
+ int lo = 0 ;
228
+ int hi = m;
229
+ while (lo <= hi) {
230
+ // partition A position i
231
+ int i = lo + (hi - lo) / 2 ;
232
+ // partition B position j
233
+ int j = (m + n + 1 ) / 2 - i;
234
+
235
+ int maxLeftA = i == 0 ? Integer . MIN_VALUE : nums1[i - 1 ];
236
+ int minRightA = i == m ? Integer . MAX_VALUE : nums1[i];
237
+
238
+ int maxLeftB = j == 0 ? Integer . MIN_VALUE : nums2[j - 1 ];
239
+ int minRightB = j == n ? Integer . MAX_VALUE : nums2[j];
240
+
241
+ if (maxLeftA <= minRightB && maxLeftB <= minRightA) {
242
+ // total length is even
243
+ if ((m + n) % 2 == 0 ) {
244
+ return (double ) (Math . max(maxLeftA, maxLeftB) + Math . min(minRightA, minRightB)) / 2 ;
245
+ } else {
246
+ // total length is odd
247
+ return (double ) Math . max(maxLeftA, maxLeftB);
248
+ }
249
+ } else if (maxLeftA > minRightB) {
250
+ // binary search left half
251
+ hi = i - 1 ;
252
+ } else {
253
+ // binary search right half
254
+ lo = i + 1 ;
255
+ }
256
+ }
257
+ return 0.0 ;
258
+ }
259
+ }
260
+ ```
261
+ *** 复杂度分析***
262
+ - 时间复杂度:$O(log(min(m, n)))$
263
+ - 空间复杂度:$O(log(min(m, n)))$
264
+
265
+
266
+ 更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经30K star啦。
267
+
268
+ 大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
269
+
270
+ ![ ] ( https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502 )
0 commit comments