Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 29, 2025. It is now read-only.

Commit 8e70bba

Browse files
Merge pull request #36 from PalmaAnd/32-0088-merge-sorted-array
32 0088 merge sorted array
2 parents add6591 + 1f2e467 commit 8e70bba

File tree

8 files changed

+214
-25
lines changed

8 files changed

+214
-25
lines changed

‎Java/.idea/workspace.xml‎

Lines changed: 40 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Java/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<dependency>
1919
<groupId>org.junit.jupiter</groupId>
2020
<artifactId>junit-jupiter</artifactId>
21-
<version>RELEASE</version>
21+
<version>5.10.0</version>
2222
<scope>test</scope>
2323
</dependency>
2424
</dependencies>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
* @param nums1 the first sorted array with nums1.length - n - 1 space at the end for merged elements
13+
* @param m the number of elements in nums1
14+
* @param nums2 the second sorted array
15+
* @param n the number of elements in nums2
16+
*/
17+
public void merge(int[] nums1, int m, int[] nums2, int n) {
18+
int nums1Index = m - 1;
19+
int nums2Index = n - 1;
20+
int mergedArrIndex = nums1.length - 1;
21+
22+
/*
23+
* The idea is to fill nums1 from the back to the front by comparing
24+
* the biggest remaining elements in nums1 and nums2
25+
*/
26+
while (nums1Index >= 0 && nums2Index >= 0) {
27+
if (nums1[nums1Index] > nums2[nums2Index]) {
28+
nums1[mergedArrIndex] = nums1[nums1Index];
29+
nums1Index--;
30+
} else {
31+
nums1[mergedArrIndex] = nums2[nums2Index];
32+
nums2Index--;
33+
}
34+
mergedArrIndex--;
35+
}
36+
37+
// If there are still elements in nums2 place them at the remaining spots of nums1
38+
while (nums2Index >= 0) {
39+
nums1[mergedArrIndex] = nums2[nums2Index];
40+
nums2Index--;
41+
mergedArrIndex--;
42+
}
43+
}
44+
}

‎Java/src/main/java/problems/leetcode/stack/ReversePolish.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ private static boolean isNumeric(String str){
4141
Integer.parseInt(str);
4242
return true;
4343
} catch (Exception e){
44-
System.out.println("Error occured: " + e.toString());
4544
return false;
4645
}
4746
}

‎Java/src/test/java/problems/leetcode/arrays_hashing/GroupAnagramsTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.*;
66

77
import static org.junit.jupiter.api.Assertions.assertEquals;
8-
import static org.junit.jupiter.api.Assertions.assertTrue;
98

109
public class GroupAnagramsTest {
1110

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package problems.leetcode.arrays_hashing;
2+
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
public class MergeSortedArraysTest {
6+
7+
@Test
8+
public void testMergeExample1() {
9+
MergeSortedArrays mergeSortedArrays = new MergeSortedArrays();
10+
11+
int[] nums1 = {1, 2, 3, 0, 0, 0};
12+
int[] nums2 = {2, 5, 6};
13+
int[] expected = {1, 2, 2, 3, 5, 6};
14+
mergeSortedArrays.merge(nums1, 3, nums2, 3);
15+
assertArrayEquals(expected, nums1);
16+
}
17+
18+
@Test
19+
public void testMergeExample2() {
20+
MergeSortedArrays mergeSortedArrays = new MergeSortedArrays();
21+
22+
int[] nums1 = {1};
23+
int[] nums2 = {};
24+
int[] expected = {1};
25+
mergeSortedArrays.merge(nums1, 1, nums2, 0);
26+
assertArrayEquals(expected, nums1);
27+
}
28+
29+
@Test
30+
public void testMergeExample3() {
31+
MergeSortedArrays mergeSortedArrays = new MergeSortedArrays();
32+
33+
int[] nums1 = {0};
34+
int[] nums2 = {1};
35+
int[] expected = {1};
36+
mergeSortedArrays.merge(nums1, 0, nums2, 1);
37+
assertArrayEquals(expected, nums1);
38+
System.out.println("Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.");
39+
}
40+
41+
@Test
42+
public void testMergeCustom1() {
43+
MergeSortedArrays mergeSortedArrays = new MergeSortedArrays();
44+
45+
int[] nums1 = {5, 6, 7, 0, 0, 0};
46+
int[] nums2 = {1, 2, 3};
47+
int[] expected = {1, 2, 3, 5, 6, 7};
48+
mergeSortedArrays.merge(nums1, 3, nums2, 3);
49+
assertArrayEquals(expected, nums1);
50+
}
51+
52+
@Test
53+
public void testEmptyArrays() {
54+
MergeSortedArrays mergeSortedArrays = new MergeSortedArrays();
55+
56+
int[] nums1 = {};
57+
int[] nums2 = {};
58+
int[] expected = {};
59+
mergeSortedArrays.merge(nums1, 0, nums2, 0);
60+
assertArrayEquals(expected, nums2);
61+
}
62+
}

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Here is a list of the problems I've solved along with links to the corresponding
3333
| [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [Java](Java/src/main/java/problems/leetcode/arrays_hashing/GroupAnagrams.java) |
3434
| [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [Java](Java/src/main/java/problems/leetcode/arrays_hashing/ProductExceptSelf.java) |
3535
| [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [Java](Java/src/main/java/problems/leetcode/arrays_hashing/TopKFrequent.java) |
36+
| [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Java](Java/src/main/java/problems/leetcode/arrays_hashing/MergeSortedArrays.java) |
3637
| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Java](Java/src/main/java/problems/leetcode/linked_list/MergeTwoSortedLists.java) |
3738
| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Java](Java/src/main/java/problems/leetcode/linked_list/ReverseLinkedList.java) |
3839
| [Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) | [Java](Java/src/main/java/problems/leetcode/stack/DailyTemperatures.java) |
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Intuition
2+
My first thought was processing the `nums1`array from the back to the front. This approach involves using three pointers:
3+
- one pointer to access the last not processed entry of the **original** `num1`
4+
- another pointer to do the same thing for `nums2`
5+
- and a last one to point at the index of `nums1` that will be modified
6+
7+
We compare elements from both arrays and fill in the larger element at the end of the nums1 array. This approach idoesn't require additional space by using the given space in nums1.
8+
9+
# Approach
10+
The idea is to merge the two arrays into nums1 by iterating backward and filling in the largest elements. We use three pointers:
11+
- `nums1Index` points to the last element in the original nums1 array.
12+
- `nums2Index` points to the last element in the nums2 array.
13+
- `mergedArrIndex` points to the last position in the merged array nums1.
14+
15+
We compare the elements at `nums1[nums1Index]` and `nums2[nums2Index]`, filling in the larger element at `nums1[mergedArrIndex]`. We continue this process, moving pointers accordingly, until all elements are merged.
16+
17+
# Complexity
18+
- Time complexity: $O(m + n)$ where m is the length of nums1 and n is the length of nums2.
19+
- Space complexity: $O(1)$ as we are using constant extra space.
20+
21+
22+
```java
23+
class Solution {
24+
/**
25+
* The final sorted array should not be returned by the function,
26+
* but instead be stored inside the array nums1.
27+
* To accommodate this, nums1 has a length of m + n, where the first m elements denote the
28+
* elements that should be merged, and the last n elements are set to 0
29+
* and should be ignored. nums2 has a length of n.
30+
*
31+
* @param nums1 the first sorted array with nums1.length - n - 1 space at the end for merged elements
32+
* @param m the number of elements in nums1
33+
* @param nums2 the second sorted array
34+
* @param n the number of elements in nums2
35+
*/
36+
public void merge(int[] nums1, int m, int[] nums2, int n) {
37+
int nums1Index = m - 1;
38+
int nums2Index = n - 1;
39+
int mergedArrIndex = nums1.length - 1;
40+
41+
/*
42+
* The idea is to fill nums1 from the back to the front by comparing
43+
* the biggest remaining elements in nums1 and nums2
44+
*/
45+
while (nums1Index >= 0 && nums2Index >= 0) {
46+
if (nums1[nums1Index] > nums2[nums2Index]) {
47+
nums1[mergedArrIndex] = nums1[nums1Index];
48+
nums1Index--;
49+
} else {
50+
nums1[mergedArrIndex] = nums2[nums2Index];
51+
nums2Index--;
52+
}
53+
mergedArrIndex--;
54+
}
55+
56+
// If there are still elements in nums2 place them at the remaining spots of nums1
57+
while (nums2Index >= 0) {
58+
nums1[mergedArrIndex] = nums2[nums2Index];
59+
nums2Index--;
60+
mergedArrIndex--;
61+
}
62+
}
63+
}
64+
```
65+
66+
If this solution helped you, you can check out all of my code on GitHub [here](https://github.com/PalmaAnd/Programming-Problems).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /