【发布时间】:2019-11-15 02:09:23
【问题描述】:
我想创建一个数组,其位置的总和作为结果给出特定数字。
较大的数字必须从数组的开头填充,如果需要则填充为 0,并尽可能均匀地分布。
例如一些场景的预期结果:
const array = new Array(4);
const number = 4;
// Expected result -> [1, 1, 1 ,1]
const array = new Array(5);
const number = 17;
// Expected result -> [4, 4, 3, 3 ,3]
const array = new Array(7);
const number = 2;
// Expected result -> [1, 1, 0, 0, 0, 0, 0]
使用 Javascript 获取此数组的最有效方法是什么?
请注意,这些示例的数量很少。一个真实的例子会使用更大的数字,例如:
const array = new Array(52);
const number = 3215654;
关于可能的重复问题,这个问题需要与其他用户指出的问题不同的结果顺序,因此这里的逻辑不可行。
【问题讨论】:
-
如果您在一定的性能成本下欣赏简洁,您可能会喜欢this。
标签: javascript arrays node.js