Golang straightforward solution iteratively inserting an element from nums2 to nums1

  • 0

    In Go, we have a helpful function copy which copies elements from a source array to a destination one. We can also apply a cut of an array, so in Go, inserting an element is relatively easy.

    func merge(nums1 []int, m int, nums2 []int, n int) {
    	if m == 0 {
    		copy(nums1[:n], nums2[:n])
    	start := 0
    	for i := 0; i < n; i++ {
    		for j := start; j < m; j++ {
    			if nums1[j] >= nums2[i] {
                                    // insert nums2[i] to j th index of nums1
    				copy(nums1[j+1:], nums1[j:]) // shift nums1[j:] by one to right
    				nums1[j] = nums2[i]
    				start, m = j, m+1
    				continue OUTER
    		copy(nums1[m:m+n-i], nums2[i:])

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.