Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active September 20, 2019 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/4af3e5158b3eff77a360d48aea844ca5 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/4af3e5158b3eff77a360d48aea844ca5 to your computer and use it in GitHub Desktop.
merge-sorted-array
import "sort"
// https://tour.golang.org/moretypes/7
// https://gobyexample.com/sorting
/*
Runtime: 0 ms, faster than 100.00% of Go online submissions for Merge Sorted Array.
Memory Usage: 3.6 MB, less than 66.67% of Go online submissions for Merge Sorted Array
https://leetcode.com/problems/merge-sorted-array/submissions/
*/
func merge(a []int, m int, b []int, n int) {
x := append(a[0:m],b[0:n]...)
sort.Ints(x)
for key,value := range x {
a[key] = value
}
}
/**
* @param {number[]} nums1
* @param {number} m
* @param {number[]} nums2
* @param {number} n
* @return {void} Do not return anything, modify nums1 in-place instead.
Runtime: 56 ms, faster than 67.02% of JavaScript online submissions for Merge Sorted Array.
Memory Usage: 35.1 MB, less than 7.69% of JavaScript online submissions for Merge Sorted Array.
https://leetcode.com/problems/merge-sorted-array/submissions/
*/
var merge = function(a, m, b, n) {
[].concat(a.slice(0,m),b.slice(0,n)).sort((a,b) => a - b)
.forEach((v,k) => a[k] = v)
return a
};
'''
Runtime: 44 ms, faster than 71.97% of Python3 online submissions for Merge Sorted Array.
Memory Usage: 13.8 MB, less than 6.15% of Python3 online submissions for Merge Sorted Array.
https://leetcode.com/problems/merge-sorted-array/submissions/
'''
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
x = nums1[0:m] + nums2[0:n]
x.sort()
for key, value in enumerate(x):
nums1[key] = value
# https://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/
# https://www.geeksforgeeks.org/python-list-sort/
# https://stackoverflow.com/questions/7696924/way-to-create-multiline-comments-in-python
# Runtime: 32 ms, faster than 89.78% of Ruby online submissions for Merge Sorted Array.
# Memory Usage: 9.5 MB, less than 100.00% of Ruby online submissions for Merge Sorted Array.
# https://leetcode.com/problems/merge-sorted-array/submissions/
def merge(nums1, m, nums2, n)
(nums1[0...m]+nums2[0...n]).sort.each_with_index do |value, index|
nums1[index] = value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment