【发布时间】:2018-02-25 06:31:20
【问题描述】:
我有一个N 元素数组,这个数组包含0、1 或nil。
我想获取存在nil 的所有索引或对数组进行排序,以便所有nil 排在第一位。
我正在寻找一种有效的方法,因为数组大小可能非常大。
这是我的代码
array_of data # array with lots of 1, 0 and nil
temp = []
array_of_data.each_with_index {|a,i| (array_of_data[i] ? true : temp << i )}
【问题讨论】:
-
CPU 高效还是内存高效?
-
如果可能的话,更重要的是 CPU 效率。
-
是否需要保留原始数组?
-
其实我会尝试两种方式保留原始数组而不是保留它。
标签: arrays ruby performance sorting memory-efficient