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

Function merge0

merge_sorted_array_88/solution.go:56–77  ·  view source on GitHub ↗

Note: original solution with array shifting

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

Source from the content-addressed store, hash-verified

54
55// Note: original solution with array shifting
56func merge0(nums1 []int, m int, nums2 []int, n int) {
57 slot := 0
58
59 // for each in nums2, insert into nums1
60 for i := 0; i < n; i++ {
61
62 // find the slot to insert nums2[i] into
63 for slot < m+i && nums1[slot] <= nums2[i] {
64 slot++
65 }
66
67 // count how many shifts are needed
68 shiftCnt := m - slot + 1 + i
69
70 // shift over each number to insert nums2[i]
71 for s := 0; s < shiftCnt; s++ {
72 hold := nums2[i]
73 nums2[i] = nums1[slot+s]
74 nums1[slot+s] = hold
75 }
76 }
77}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected