-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode88.java
More file actions
34 lines (29 loc) · 852 Bytes
/
LeetCode88.java
File metadata and controls
34 lines (29 loc) · 852 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
public class LeetCode88 {
public static void main(String[] args) {
int nums1[] = {1,2,3,0,0,0};
int nums2[] = {2,5,6};
int m = 3;
int n = 3;
mergeArrays(nums1, nums2, m, n);
//It's just for only to see the printing output
for(int i = 0; i < m+n; i++) {
System.out.printf(nums1[i] + " ");
}
}
public static void mergeArrays(int [] nums1, int [] nums2, int m, int n) {
int i = m - 1;
int j = n - 1;
int k = m + n - 1;
while (j >= 0) {
if (i >= 0 && nums1[i] > nums2[j]) {
nums1[k] = nums1[i];
k--;
i--;
} else {
nums1[k] = nums2[j];
k--;
j--;
}
}
}
}