LeetCode: Merge Sorted Array
Question
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.
Solution #1
def merge(nums1, m, nums2, n)
while i < m + n
nums1[i + m] = nums2[i] if nums2[i]
i += 1
end
bubble_sort(nums1)
end
def bubble_sort(nums)
j = 0
i = 0
while j < nums.length - 1
while i < nums.length - 1
nums[i], nums[i + 1] = nums[i + 1], nums[i] if nums[i + 1] && nums[i] > nums[i + 1]
i += 1
end
i = 0
j += 1
end
nums
end
Comments