MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / merge

Function merge

merge_sorted_array_88/solution.go:4–20  ·  view source on GitHub ↗

O(m+n) solution

(nums1 []int, m int, nums2 []int, n int)

Source from the content-addressed store, hash-verified

2
3// O(m+n) solution
4func merge(nums1 []int, m int, nums2 []int, n int) {
5 if n == 0 {
6 return
7 }
8
9 n1, n2, p := m-1, n-1, m+n-1
10 for n2 > -1 {
11 if n1 < 0 || nums2[n2] >= nums1[n1] {
12 nums1[p] = nums2[n2]
13 n2--
14 } else {
15 nums1[p] = nums1[n1]
16 n1--
17 }
18 p--
19 }
20}
21
22// Note: solution from rotate_string_796 interview that stumped me for a bit.
23// Wanted to solve without shifting and was tricky.

Callers 1

Test_mergeFunction · 0.70

Calls

no outgoing calls

Tested by 1

Test_mergeFunction · 0.56