Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d605b3d

Browse files
authored
feat(js-solution): add js-solution for problem 4 (azl397985856#337)
1 parent b327213 commit d605b3d

File tree

1 file changed

+81
-8
lines changed

1 file changed

+81
-8
lines changed

problems/4.median-of-two-sorted-array.md

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ The median is (2 + 3)/2 = 2.5
3535
#### 解法一 - 暴力 (Brute Force)
3636
暴力解主要是要merge两个排序的数组`(A,B)`成一个排序的数组。
3737

38-
用两个`pointer(i,j)``i` 从数组`A`起始位置开始,即`i=0`开始,`j` 从数组`B`起始位置, 即`j=0`开始.
39-
一一比较 `A[i] 和 B[j]`,
38+
用两个`pointer(i,j)``i` 从数组`A`起始位置开始,即`i=0`开始,`j` 从数组`B`起始位置, 即`j=0`开始.
39+
一一比较 `A[i] 和 B[j]`,
4040
1. 如果`A[i] <= B[j]`, 则把`A[i]` 放入新的数组中,i往后移一位,即 `i+1`.
4141
2. 如果`A[i] > B[j]`, 则把`B[j]` 放入新的数组中,j往后移一位,即 `j+1`.
4242
3. 重复步骤#1#2,直到`i`移到`A`最后,或者`j`移到`B`最后。
43-
4. 如果`j`移动到`B`数组最后,那么直接把剩下的所有`A`依次放入新的数组中.
43+
4. 如果`j`移动到`B`数组最后,那么直接把剩下的所有`A`依次放入新的数组中.
4444
5. 如果`i`移动到`A`数组最后,那么直接把剩下的所有`B`依次放入新的数组中.
4545

4646
Merge的过程如下图。
@@ -76,7 +76,7 @@ Merge的过程如下图。
7676
1. 暴力求解,在线性时间内merge两个排好序的数组成一个数组。
7777
2. 二分查找,关键点在于
7878
- 要partition两个排好序的数组成左右两等份,partition需要满足`len(Aleft)+len(Bleft)=(m+n+1)/2 - m是数组A的长度, n是数组B的长度`
79-
79+
8080
- 并且partition后 A左边最大(`maxLeftA`), A右边最小(`minRightA`), B左边最大(`maxLeftB`), B右边最小(`minRightB`) 满足
8181
`(maxLeftA <= minRightB && maxLeftB <= minRightA)`
8282

@@ -127,7 +127,7 @@ class MedianTwoSortedArrayBruteForce {
127127
}
128128
}
129129
```
130-
*解法二 - 二分查找(Binary Search*
130+
*解法二 - 二分查找(Binary Search)*
131131
```java
132132
class MedianSortedTwoArrayBinarySearch {
133133
public static double findMedianSortedArraysBinarySearch(int[] nums1, int[] nums2) {
@@ -144,13 +144,13 @@ class MedianSortedTwoArrayBinarySearch {
144144
int i = lo + (hi - lo) / 2;
145145
// partition B position j
146146
int j = (m + n + 1) / 2 - i;
147-
147+
148148
int maxLeftA = i == 0 ? Integer.MIN_VALUE : nums1[i - 1];
149149
int minRightA = i == m ? Integer.MAX_VALUE : nums1[i];
150-
150+
151151
int maxLeftB = j == 0 ? Integer.MIN_VALUE : nums2[j - 1];
152152
int minRightB = j == n ? Integer.MAX_VALUE : nums2[j];
153-
153+
154154
if (maxLeftA <= minRightB && maxLeftB <= minRightA) {
155155
// total length is even
156156
if ((m + n) % 2 == 0) {
@@ -171,3 +171,76 @@ class MedianSortedTwoArrayBinarySearch {
171171
}
172172
}
173173
```
174+
175+
## 代码 (javascript code)
176+
*解法一 - 暴力解法(Brute force)*
177+
```js
178+
/**
179+
* @param {number[]} nums1
180+
* @param {number[]} nums2
181+
* @return {number}
182+
*/
183+
var findMedianSortedArrays = function(nums1, nums2) {
184+
// 归并排序
185+
const merged = []
186+
let i = 0
187+
let j = 0
188+
while(i < nums1.length && j < nums2.length) {
189+
if (nums1[i] < nums2[j]) {
190+
merged.push(nums1[i++])
191+
} else {
192+
merged.push(nums2[j++])
193+
}
194+
}
195+
while(i < nums1.length) {
196+
merged.push(nums1[i++])
197+
}
198+
while(j < nums2.length) {
199+
merged.push(nums2[j++])
200+
}
201+
202+
const { length } = merged
203+
return length % 2 === 1
204+
? merged[Math.floor(length / 2)]
205+
: (merged[length / 2] + merged[length / 2 - 1]) / 2
206+
};
207+
```
208+
209+
*解法二 - 二分查找(Binary Search)*
210+
```js
211+
/**
212+
* 二分解法
213+
* @param {number[]} nums1
214+
* @param {number[]} nums2
215+
* @return {number}
216+
*/
217+
var findMedianSortedArrays = function(nums1, nums2) {
218+
// make sure to do binary search for shorten array
219+
if (nums1.length > nums2.length) {
220+
[nums1, nums2] = [nums2, nums1]
221+
}
222+
const m = nums1.length
223+
const n = nums2.length
224+
let low = 0
225+
let high = m
226+
while(low <= high) {
227+
const i = low + Math.floor((high - low) / 2)
228+
const j = Math.floor((m + n + 1) / 2) - i
229+
230+
const maxLeftA = i === 0 ? -Infinity : nums1[i-1]
231+
const minRightA = i === m ? Infinity : nums1[i]
232+
const maxLeftB = j === 0 ? -Infinity : nums2[j-1]
233+
const minRightB = j === n ? Infinity : nums2[j]
234+
235+
if (maxLeftA <= minRightB && minRightA >= maxLeftB) {
236+
return (m + n) % 2 === 1
237+
? Math.max(maxLeftA, maxLeftB)
238+
: (Math.max(maxLeftA, maxLeftB) + Math.min(minRightA, minRightB)) / 2
239+
} else if (maxLeftA > minRightB) {
240+
high = i - 1
241+
} else {
242+
low = low + 1
243+
}
244+
}
245+
};
246+
```

0 commit comments

Comments
 (0)