NodeJS 返回一个数组,其中包含所有2的幂的索引

0ve6wy6x  于 2023-08-04  发布在  Node.js
关注(0)|答案(8)|浏览(98)

我正在解决这个问题。数学不是一个很强的套件。任何提示将是伟大的。它应该返回一个索引为2的幂的数组。

function secondPower(arr) {
    // Return an array containing all indices that are powers of 2
    newArray = [];

    for(let i = 0; i < arr.length; i++){
        if(arr[i] % (2 ** i) === 0  && arr[i] != 1){
            newArray.push(arr[i]);
        }
    }
    return newArray;
}

字符串
解决方案的一个示例是

secondPower([1, 2, 3, 4, 5, 6, 7, 8])


退货

[2,3,5]

f0brbegy

f0brbegy1#

可以从索引1开始,不断乘以2,直到值达到数组的长度。该解决方案在对数时间内运行,避免了所有索引上的线性循环。

function secondPower(arr) {
    const res = [];
    for(let i = 1; i < arr.length; i <<= 1) res.push(arr[i]);
    return res;
}
console.log(secondPower([1, 2, 3, 4, 5, 6, 7, 8]));

字符串

kgsdhlau

kgsdhlau2#

起初,我很难理解这是在问什么,但这返回了指数,并为我工作。

function secondPower(arr) {
// Return an array containing all indices that are powers of 2
// Double the value of i each time through the loop starting at 1
let newArr = [];
for (let i = 1; i < arr.length; i *= 2) {
        newArr.push(arr[i]);
    }
    return newArr;
}

console.log(secondPower([1, 2, 3, 4, 5, 6, 7, 8]));

字符串

mhd8tkvw

mhd8tkvw3#

使用What is the best way to determine if a given number is a power of two?

const isPowerOf2 = v => v && !(v & (v - 1));
[1, 2, 3, 4, 5, 6, 7, 8].filter(isPowerOf2);

字符串
产量

[1, 2, 4, 8]

sr4lhrrt

sr4lhrrt4#

// if a number is a power of 2 its base 2 logarithm is an integer
const result = [1, 2, 3, 4, 5, 6, 7, 8].reduce((a, v, i) => Number.isInteger(Math.log2(v)) ? a.concat(i) : a, []);
console.log(result);

字符串

jxct1oxe

jxct1oxe5#

function secondPower(arr) {

// return an array containing all indices that are powers of 2
// your code here 

let array = [];
  
for(let i=0;i<arr.length;i++) {
    
  if(arr[2**i]) {
     array.push(arr[2**i]);
  }
    
 }
    
 return array;

}

字符串

4jb9z9bj

4jb9z9bj6#

这就是我最终得出的结论,使用Math.pow(x,y)作为loopEnd表达式-其中x是y的幂。

function secondPower(arr) {
  let powersOf2 = [];
  let num = 0;
  for (let i = 1; i < arr.length; i = Math.pow(2, num += 1)) {
    powersOf2.push(arr[i]);
  } return powersOf2;
}

console.log(secondPower([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 
120, 130, 140, 150, 160, 170, 180]));

字符串
产量:[ 20,30,50,90,170 ]

rqenqsqc

rqenqsqc7#

提示:Math.log2()静态方法返回一个数字的以2为底的对数。
取指数的以2为底的对数,以确定指数是否为2的幂
检查对数的结果是否为整数(无小数部分)。如果结果是整数,则索引是2的幂。因此,如果结果被计算为小数,那么它将为false
注意:因为数组从索引0开始,如果我们执行Math.log2,它将导致'-infinity',因此我们可以从索引= 1开始。

// example: 
// if i = 1, then 'Math.log2(1)' is 0 because 2^0 = 1, in this case Number.isInteger(0) returns true
// if i = 2, them 'Math.log2(2)' is 1 becuase 2^1 = 2, in this case Number.isInteger(1) returns true;
// if i = 3, then 'Math.log2(3)' is approximately 1.58496250072115 and Number.isInteger returns false indicating that the index of 3 is not a power of 2

function secondPower(arr) {
  let output = []; 
  for (let i = 0; i < arr.length; i++) {
    if (Number.isInteger(Math.log2(i))) {
      output.push(arr[i])
    }
  }
  return output; 
}

字符串

6yjfywim

6yjfywim8#

你可以使用按位运算符,这是很多更快。但它们只适用于2的幂:

function secondPower(arr) {
    var result = [];
    for (var k = 0; k < arr.length; k++) {
        if (isPower(k)) { result.push(arr[k]); }
    }
    return result;
}

//... this function checks if there is only one 1 on the binary number
function isPower(k) {
    var found = false;
    while (k > 0) {
        if (k & 1 == 1) {
            if (found) { return false; }
            found = true;
        }
        k >>= 1;
    }
    return found;
}

secondPower([1, 2, 3, 4, 5, 6, 7, 8]);

字符串

相关问题