forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortedArray.java
More file actions
19 lines (18 loc) · 539 Bytes
/
MergeSortedArray.java
File metadata and controls
19 lines (18 loc) · 539 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MergeSortedArray {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int j = m - 1, k = n - 1;
for (int i = m + n - 1; i >= 0; i--) {
if (j < 0) {
nums1[i] = nums2[k--];
} else if (k < 0) {
nums1[i] = nums1[j--];
} else {
if (nums1[j] >= nums2[k]) {
nums1[i] = nums1[j--];
} else {
nums1[i] = nums2[k--];
}
}
}
}
}