一道很简单的算法题,写着玩的
方法是写出来了,但是目前有一个问题就是 数据量大了 就会内存溢出,还需要想一想怎么办
function fun(m = 0, n = 0) { // leftBny 左侧边界 rightBny 右侧边界 let [leftBny, rightBny] = m < n ? [m, n] : [n, m] let startNum = leftBny, //起始位置 endNum = leftBny, //结束位置 sumNum = leftBny + leftBny, ret = [] function _loop() { while (true) { if (sumNum === rightBny) { ret.push({ startNum, endNum, sumNum }) break; } if (sumNum > rightBny) { break; } ret.push({ startNum, endNum, sumNum }) endNum++ sumNum = startNum + endNum } } _loop() while (true) { endNum = ++startNum sumNum = startNum + endNum if (startNum > Math.floor(rightBny / 2)) { break } _loop() } console.log(ret)}fun(3, 100000)