Swift solution - O(n) time & O(1) space


  • 0
    class Solution {
        func merge(_ nums1: inout [Int], _ m: Int, _ nums2: [Int], _ n: Int) {
            if m == 0 {
                nums1 = nums2
            }
            if n == 0 {
                return
            }
            
            var i = m - 1
            var j = n - 1
            var k = m + n - 1
            
            while i >= 0 && j >= 0 {
                if nums1[i] >= nums2[j] {
                    nums1[k] = nums1[i]
                    k -= 1
                    i -= 1
                } else {
                    nums1[k] = nums2[j]
                    k -= 1
                    j -= 1
                }
            }
            
            while j >= 0 {
                nums1[k] = nums2[j]
                k -= 1
                j -= 1
            }
        }
    }
    

Log in to reply
 

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