反向数组顺序 [英] Reverse Array Order

查看:178
本文介绍了反向数组顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扭转Java中的数组的顺序。结果
这是在O(n)的与所用的存储器量最少这样做的最有效的方法。结果,
无需用code回答,伪code将被罚款。结果
这里是我的思维过程:

I am trying to reverse the order of an Array in java.
What is the most efficient way to do so in O(n) with the least amount of memory used.
No need to answer with code, pseudo code will be fine.
Here is my thought process:

  create a new temp array //I think this is a waste of memory, 
                          //but I am not sure if there's a better way
 grab elements from the end of the original array -decrement this variable
 insert element in beginning of temp array -increment this variable
then make the original array point to the temp array? //I am not sure 
            //if I can do this in java; so let's say the 
            //original array is Object[] arr; and the temp array is 
            //Object[] temp. Can I do temp = arr; ?

有没有更好的更有效的方法,而无需使用临时数组也许这样做呢?
最后,假设有数组中没有空,所以一切都可以正常工作。
谢谢

Is there a better more efficient way to do this perhaps without using a temp array? and Lastly, assume that there are no nulls in the array, so everything can work. Thank you

编辑:不,这不是功课

推荐答案

如果它是一个对象数组,那么 Col​​lections.reverse(Arrays.asList(阵列))将尽恒内存和线性时间的工作 - 不需要临时数组

If it's an Object array, then Collections.reverse(Arrays.asList(array)) will do the job with constant memory and linear time -- no temporary array required.

这篇关于反向数组顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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