我在尝试用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; ?
有没有更好更有效的方法来做这件事,也许不需要使用临时数组? 最后,假设数组中没有空值,那么一切都能正常工作。 谢谢
编辑:不,这不是作业。
Collections.reverseOrder()
吗? - Churk