我正在尝试反转 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.reverse(Arrays.asList(array))
将以恒定的内存和线性时间完成这项工作——不需要临时数组。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)