我想在java中颠倒数组的顺序。
在O(n)中使用最少内存量的最有效方式是什么?
不需要用代码来回答,伪代码会很好。
这里是我的思维过程:颠倒阵列顺序
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; ?
有没有更好的更有效的方法,而无需使用临时数组也许这样做呢? 最后,假设数组中没有空值,所以一切都可以工作。 谢谢
编辑:不,这不是功课。
这功课吗?如果是的话,请标记为这样。 – 2012-04-03 14:35:02
考虑交换第一个和最后一个项目,然后是第二个和第二个项目,直到达到列表的一半。你只需要一个临时变量,并且仍然会在列表中一次? – 2012-04-03 14:36:01
http://stackoverflow.com/questions/2137755/how-do-i-reverse-an-int-array-in-java – 2012-04-03 14:36:02