console
import {combinationSum2} from './demo';
test("只有一个数输出空", () => { expect(combinationSum2([10], 8)).toEqual([]) });
test("只有一个数输出本身", () => { expect(combinationSum2([10], 10)).toEqual([[10]]) });
test("两个数输出本身", () => { expect(combinationSum2([2,6], 8)).toEqual([[2,6]]) });
test("两个数输出空", () => { expect(combinationSum2([2,6], 9)).toEqual([]) });
test("两个数输出其中一个", () => { expect(combinationSum2([2,6], 6)).toEqual([[6]]) });
test("三个数输出其中本身", () => { expect(combinationSum2([2,4,6], 12)).toEqual([[2,4,6]]) });
test("三个数输出其中空", () => { expect(combinationSum2([2,4,6], 20)).toEqual([]) });
test("三个数输出", () => { expect(combinationSum2([2,4,6], 6)).toEqual([[2,4], [6]]) });
test("三个数有重复数输出", () => { expect(combinationSum2([2,2,6], 8)).toEqual([[2,6] ]) });
test("五个数有重复数输出", () => {
expect(combinationSum2([2,5,2,1,2], 5)).toEqual([[1,2,2], [5]])
});
test("七个数有重复数输出", () => {
expect(combinationSum2([10,1,2,7,6,1,5], 8)).toEqual([[1, 7], [1, 2, 5], [2, 6], [1, 1, 6]])
});
test("所有数相等", () => {
expect(combinationSum2([2,2,2,2,2,2,2,2], 10)).toEqual([[2,2,2,2,2]])
});
// 其他matcher
// expect(..).not.toBe()
// expect(..).toBeNull()
// expect(..).toBeUndefined()
// expect(..).toBeDefined()
// expect(..).toBeTruthy()
// expect(..).toBeFalsy()
// expect(..).toBeGreaterThan()
// expect(..).toBeLessThan()
// expect(..).toBeGreaterThanOrEqual()
// expect(..).toBeLessThanOrEqual()
// expect(..).toMatch()
// expect(..).toContain()
import {combinationSum} from './demo';
test("只有一个数输出空", () => { expect(combinationSum([10], 8)).toEqual([]) });
test("只有一个数输出本身", () => { expect(combinationSum([10], 10)).toEqual([[10]]) });
test("两个数输出本身", () => { expect(combinationSum([2,6], 8)).toEqual([[2,2,2,2], [2,6]]) });
test("两个数输出空", () => { expect(combinationSum([2,6], 9)).toEqual([]) });
test("两个数输出其中一个", () => { expect(combinationSum([2,6], 6)).toEqual([[2,2,2], [6]]) });
test("三个数输出其中本身", () => {
expect(combinationSum([2,4,6], 12)).toEqual([
[2,2,2,2,2,2],[2,2,2,2,4],[2,2,2,6],[2,2,4,4],[2,4,6],[4,4,4],[6,6]])
});
test("四个数输出其中本身", () => {
expect(combinationSum([2,3,6,7], 7)).toEqual([
[7], [2,2,3]])
});
test("五个数有重复数输出", () => {
expect(combinationSum([2,5,2,1,2], 5)).toEqual([
[1,1,1,1,1],[1,1,1,2],[1,2,2],[5]])
});
test("所有数相等", () => {
expect(combinationSum([2,2,2,2,2,2,2,2], 10)).toEqual([[2,2,2,2,2]])
});
// 其他matcher
// expect(..).not.toBe()
// expect(..).toBeNull()
// expect(..).toBeUndefined()
// expect(..).toBeDefined()
// expect(..).toBeTruthy()
// expect(..).toBeFalsy()
// expect(..).toBeGreaterThan()
// expect(..).toBeLessThan()
// expect(..).toBeGreaterThanOrEqual()
// expect(..).toBeLessThanOrEqual()
// expect(..).toMatch()
// expect(..).toContain()
var candidates = [1,2,3,4,8];
var target = 8;
var combinationSum2 = function (candidates, target) {
let res = [], path = [];
candidates.sort((a, b) => a - b);
combination(candidates, target, 0, res, path);
return res;
};
function combination(candidates, target, index, res, path) {
if (target === 1) {
addRes(res, path);
} else if (target < 1) return;
for (let i = index; i < candidates.length; i++) {
var temp = target;
temp /= candidates[i];
if(Math.floor(temp) === temp) {
path.push(candidates[i]);
combination(candidates, target/candidates[i], i+1, res, path);
path.pop();
}
}
}
var addRes = function(array1, array2) {
let array = JSON.parse(JSON.stringify(array2));
for (let i = 0; i < array1.length; i++) {
if (array1[i].sort().toString() === array.sort().toString()) {
array1.splice(i, 1);
break;
}
}
array1.push(array2.slice());
}
console.log(combinationSum2(candidates,target));