Skip to content

Instantly share code, notes, and snippets.

@Sherlock-Holo
Created December 26, 2018 08:31
Show Gist options
  • Save Sherlock-Holo/ec00fd9b594669a95628a2498cba295a to your computer and use it in GitHub Desktop.
Save Sherlock-Holo/ec00fd9b594669a95628a2498cba295a to your computer and use it in GitHub Desktop.
package main
func merge(nums1 []int, m int, nums2 []int, n int) {
if n == 0 {
return
}
if m == 0 {
copy(nums1, nums2)
return
}
j := m - 1
k := n - 1
for i := len(nums1) - 1; i >= 0; i-- {
if nums1[j] > nums2[k] {
nums1[i] = nums1[j]
j--
if j < 0 {
copy(nums1, nums2[:k+1])
return
}
} else {
nums1[i] = nums2[k]
k--
if k < 0 {
return
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment