Note: solution from rotate_string_796 interview that stumped me for a bit. Wanted to solve without shifting and was tricky.
(nums1 []int, m int, nums2 []int, n int)
| 22 | // Note: solution from rotate_string_796 interview that stumped me for a bit. |
| 23 | // Wanted to solve without shifting and was tricky. |
| 24 | func merge2(nums1 []int, m int, nums2 []int, n int) { |
| 25 | if n == 0 { |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | // place largest values into the back of nums1 |
| 30 | p := len(nums1) - 1 |
| 31 | i := m - 1 |
| 32 | j := n - 1 |
| 33 | |
| 34 | // while i and j are greater than 0 |
| 35 | for i >= 0 && j >= 0 { |
| 36 | // place largest of i and j into p |
| 37 | if nums1[i] > nums2[j] { |
| 38 | nums1[p] = nums1[i] |
| 39 | i-- |
| 40 | } else { |
| 41 | nums1[p] = nums2[j] |
| 42 | j-- |
| 43 | } |
| 44 | |
| 45 | p-- |
| 46 | } |
| 47 | |
| 48 | // if j is not less than 0, fill nums1 with num2 values |
| 49 | for j >= 0 { |
| 50 | nums1[j] = nums2[j] |
| 51 | j-- |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Note: original solution with array shifting |
| 56 | func merge0(nums1 []int, m int, nums2 []int, n int) { |
nothing calls this directly
no outgoing calls
no test coverage detected