-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMergeSortedArray.java
More file actions
37 lines (37 loc) · 920 Bytes
/
MergeSortedArray.java
File metadata and controls
37 lines (37 loc) · 920 Bytes
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
package io.ziheng.array.leetcode;
/**
* LeetCode 88. Merge Sorted Array
* https://leetcode.com/problems/merge-sorted-array/
*/
public class MergeSortedArray {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int pA = 0;
int pB = 0;
int pIndex = 0;
int[] arr = new int[m];
while (pA < m && pB < n) {
if (nums1[pA] < nums2[pB]) {
arr[pIndex] = nums1[pA];
pA++;
} else {
arr[pIndex] = nums2[pB];
pB++;
}
pIndex++;
}
while (pA < m) {
arr[pIndex] = nums1[pA];
pA++;
pIndex++;
}
while (pB < n) {
arr[pIndex] = nums2[pB];
pB++;
pIndex++;
}
for (int i = 0; i < arr.length; i++) {
nums1[i] = arr[i];
}
}
}
/* EOF */