O(m+n) solution
(nums1 []int, m int, nums2 []int, n int)
| 2 | |
| 3 | // O(m+n) solution |
| 4 | func 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. |