Giter VIP home page Giter VIP logo

Comments (5)

ucev avatar ucev commented on August 23, 2024 22
 function f() {    
       var arr = Array.from(arguments);
       let before = arr.slice(0, arr.length - 1);
       let n = arr[arr.length - 1];
       for (let i = 1; i < n; i++) {
            f(...before, i, n - i);
       }
       console.log(arr);
  }

from frontend-interview.

LZ0211 avatar LZ0211 commented on August 23, 2024
//减治法,O(n2)
var resolve = (function (){
    //闭包,缓存结果到table中
    var table = [
        [[]],
    ];
    /*初始表格,省去递归的边界条件检查;
    table[0]=[[]]
    table[1]=[[1]]
    */
    function fn(n){
        //查表,如果表中不存在
        if(!table[n]){
            var arr = [];
            /*
            f(1) = 0^f(1)=[[1]]
            f(2) = 1^f(1) U 0^f(2) = [[1,1],[2]]
            对于f(n),其结果为 [n]组合f(0) + [n-1]组合f(1) + [n-2]组合f(2) + ... + [1]组合f(n-1)
            */
            for(var i=n;i>=1;i--){
                fn(n-i).forEach(sub=>arr.push([i].concat(sub)))//将f(n-i)的每一项和i组合
            }
            table[n] = arr;//缓存结果
        }
        return table[n]
    }
    return fn;
})()

from frontend-interview.

BYChoo avatar BYChoo commented on August 23, 2024

@ucev 请问下可以写下思路吗?这算法写的太好了

from frontend-interview.

luckhpy avatar luckhpy commented on August 23, 2024

这是一个循环递归调用,首先
function f(n) {
if (n === 1) return [[1]];
var rs = [[n]];
for (var i = n - 1; i > 0; i--) {
rs = rs.concat(f(n - i).map(item => [i].concat(item)))
}
return rs
}
由于本题是输出结果,那就只有把需要输出的信息带过去
function f() {
var arr = Array.from(arguments);
console.log(arr)
var len = arr.length
var fRecursion = arr[0] //用于循环
var before = arr.slice(1, len)
for (var i = fRecursion - 1; i > 0; i--) {
f(i, ...before, fRecursion-i)
}
}

from frontend-interview.

zonakaka avatar zonakaka commented on August 23, 2024

function splitNum(num) {
let res =[]
help(0, [], num)
function help(cur, path, num) {
if (cur >= num) {
cur === num && res.push(path)
return
}
for (let i = 1; i <= num; i++) {
path.push(i)
cur += i
help(cur, path.slice(), num)
console.log(cur, path)
path.pop()
cur -=i
}
}
return res

}

from frontend-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.