We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定 N 个数组,求出这些数组的笛卡尔积
function getArranging( /* listA, listB, ... */){ //TODO - 返回 listA × listB × ... } const a = [0, 1]; const b = [3, 4, 5]; const c = [6, 7]; // test expect(getArranging( a, b )).to.eql( [ [0, 3], [0, 4], [0, 5], [1, 3], [1, 4], [1, 5] ] ); expect(getArranging( a, b, c )).to.eql( [ [0,3,6], [0,3,7], [0,4,6], [0,4,7], [0,5,6], [0,5,7], [1,3,6], [1,3,7], [1,4,6], [1,4,7], [1,5,6], [1,5,7] ] );
The text was updated successfully, but these errors were encountered:
function getArranging(...args) { return args.reduce(function (a, b) { const curRes = [] a.forEach(x => { b.forEach(y => { curRes.push([].concat(x, y)) }) }) return curRes }, [[]]) }
Sorry, something went wrong.
function getArranging() { let result = arguments[0]; for (let i = 1; i < arguments.length; i++) { result = utilFn(result, arguments[i]); } return result; } function utilFn(arr1, arr2) { let arr = []; arr1.map(elt1 => { arr2.map(elt2 => { arr.push([].concat(elt1, elt2)); }) }); return arr; }
😂 我的思路跟 @yinkaihui 是一样的,另外补充一个递归的写法,盗用一下 @iLove-Coding 的 utilFn
function getArranging(listA, listB, ...rest) { if (!listB) { return listA; } return getArranging(utilFn(listA, listB), ...rest); }
No branches or pull requests
给定 N 个数组,求出这些数组的笛卡尔积
The text was updated successfully, but these errors were encountered: