Skip to content

Commit

Permalink
Continued work in MergeSortedArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
PalmaAnd committed Oct 29, 2023
1 parent 00d5716 commit 6ac2248
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
34 changes: 18 additions & 16 deletions Java/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package problems.leetcode.arrays_hashing;

public class MergeSortedArrays {

/**
* The final sorted array should not be returned by the function,
* but instead be stored inside the array nums1.
* To accommodate this, nums1 has a length of m + n, where the first m elements denote the
* elements that should be merged, and the last n elements are set to 0
* and should be ignored. nums2 has a length of n.
*/
public void merge(int[] nums1, int m, int[] nums2, int n) {
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
if (nums1[i] < nums2[j]){
moveArray(nums1, i, m);
nums1[i] = nums2[j];
}
}
}
}

private void moveArray(int[] nums1, int i, int size){
int val = nums1[i];
while (i < size - 1){
int help = nums1[i + 1];
nums1[i + 1] = val;
val = help;
i++;
}
}
}

0 comments on commit 6ac2248

Please sign in to comment.