Skip to content

Commit 3b18644

Browse files
optimize 88
1 parent 1c6e93b commit 3b18644

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ Your ideas/fixes/algorithms are more than welcome!
349349
|92|[Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)|[Solution](../master/src/main/java/com/stevesun/solutions/_92.java)| O(n)|O(1) | Medium
350350
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../master/src/main/java/com/stevesun/solutions/DecodeWays.java)| O(n)|O(n) | Medium| DP
351351
|89|[Gray Code](https://leetcode.com/problems/gray-code/)|[Solution](../master/src/main/java/com/stevesun/solutions/GrayCode.java)|O(n) |O(1)|Medium|Bit Manipulation
352+
|88|[Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)|[Solution](../master/src/main/java/com/stevesun/solutions/_88.java)|O(max(m,n)) |O(1)|Easy|
352353
|86|[Partition List](https://leetcode.com/problems/partition-list/)|[Solution](../master/src/main/java/com/stevesun/solutions/PartitionList.java)|O(?) |O(?)|Medium|
353354
|85|[Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/)|[Solution](../master/src/main/java/com/stevesun/solutions/MaximalRectangle.java)|O(m*n) |O(n)|Hard|DP
354355
|83|[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)|[Solution](../master/src/main/java/com/stevesun/solutions/_83.java)|O(n) |O(1)|Medium| Linked List

src/main/java/com/stevesun/solutions/MergeSortedArray.java renamed to src/main/java/com/stevesun/solutions/_88.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@
33
/**Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
44
55
Note:
6-
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.*/
6+
You may assume that nums1 has enough space (size that is greater or equal to m + n)
7+
to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.*/
78

8-
public class MergeSortedArray {
9+
public class _88 {
910

10-
/**I used O(m) extra space to create a temp array, but this could be avoided.*/
11+
//credit:https://discuss.leetcode.com/topic/2461/this-is-my-ac-code-may-help-you
12+
public static void merge_O1_space(int[] nums1, int m, int[] nums2, int n) {
13+
int i = m-1;
14+
int j = n-1;
15+
int k = m+n-1;
16+
while (i >= 0 && j >= 0) {
17+
if (nums1[i] > nums2[j]) nums1[k--] = nums1[i--];
18+
else nums1[k--] = nums2[j--];
19+
}
20+
while (j >= 0) nums1[k--] = nums2[j--];
21+
}
22+
23+
/**I used O(m) extra space to create a temp array, but this could be optimized.*/
1124
public static void merge(int[] nums1, int m, int[] nums2, int n) {
1225
int[] temp = new int[m];
1326
for(int i = 0; i < m; i++) temp[i] = nums1[i];

0 commit comments

Comments
 (0)