将[1,2]和[7,8]合并为[[1,7],[2,8]]的最有效方法是什么 [英] What is the most efficient way of merging [1,2] and [7,8] into [[1,7], [2,8]]

查看:27
本文介绍了将[1,2]和[7,8]合并为[[1,7],[2,8]]的最有效方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出2个数组[1,2]和[7,8],最有效的方法是将其合并以形成[[1,7],[2,8]].我知道我们可以做到:

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=[];
for (var i=0; i<a1.length; i++) {
  a3.push([a1[i], a2[i]]);
}

我正在处理各种各样的事情.所以我想看看是否有更好的方法.

I am dealing with a large array. So I want to see if there is a better way.

推荐答案

没有比O(n)更快的方法了,因为每个元素都必须被触摸一次.

There is no way to do this faster than O(n) because every element must be touched once.

这篇关于将[1,2]和[7,8]合并为[[1,7],[2,8]]的最有效方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆