What is the most efficient way of merging [1,2] and [7,8] into [[1,7], [2,8]]

前端 未结 4 1730
囚心锁ツ
囚心锁ツ 2021-01-13 15:43

Given 2 arrays [1,2] and [7,8] what is the most efficient way of merging that to form [[1,7], [2,8]]. I know we can do this:

a1 = [1,2], a2 = [7,8], a3=[];
f         


        
4条回答
  •  执念已碎
    2021-01-13 16:25

    Nope, that's about as efficient as it gets. It's running in O(n) time. Really not much more you could ask. If there's anything you could optimize, it would be transforming a1 into a map, but that's optimization for memory, and it sounds like you want to speed things up intstead.

提交回复
热议问题