Skip to content
This repository was archived by the owner on Jun 29, 2025. It is now read-only.

Commit 6ac2248

Browse files
committed
Continued work in MergeSortedArrays
1 parent 00d5716 commit 6ac2248

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

Java/.idea/workspace.xml

Lines changed: 18 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package problems.leetcode.arrays_hashing;
2+
3+
public class MergeSortedArrays {
4+
5+
/**
6+
* The final sorted array should not be returned by the function,
7+
* but instead be stored inside the array nums1.
8+
* To accommodate this, nums1 has a length of m + n, where the first m elements denote the
9+
* elements that should be merged, and the last n elements are set to 0
10+
* and should be ignored. nums2 has a length of n.
11+
*/
12+
public void merge(int[] nums1, int m, int[] nums2, int n) {
13+
for (int i = 0; i < nums1.length; i++) {
14+
for (int j = 0; j < nums2.length; j++) {
15+
if (nums1[i] < nums2[j]){
16+
moveArray(nums1, i, m);
17+
nums1[i] = nums2[j];
18+
}
19+
}
20+
}
21+
}
22+
23+
private void moveArray(int[] nums1, int i, int size){
24+
int val = nums1[i];
25+
while (i < size - 1){
26+
int help = nums1[i + 1];
27+
nums1[i + 1] = val;
28+
val = help;
29+
i++;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)