Skip to content

Commit b971826

Browse files
committed
added more
1 parent 401d499 commit b971826

16 files changed

+633
-25
lines changed

__test__/combinations-sum-iii.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import combinationSum3 from '../combinations-sum-iii';
2+
3+
describe('Combinations Sum 3', () => {
4+
5+
test('count = 3, sum = 7', () => {
6+
const result = combinationSum3(3, 7);
7+
const expected = [
8+
[1, 2, 4]
9+
];
10+
11+
expect(result).toEqual(expected);
12+
});
13+
14+
test('count = 3, sum = 9', () => {
15+
const result = combinationSum3(3, 9);
16+
const expected = [
17+
[1, 2, 6],
18+
[1, 3, 5],
19+
[2, 3, 4]
20+
];
21+
22+
expect(result).toEqual(expected);
23+
});
24+
25+
});

__test__/generate-parentheses.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import generateParentheses from '../generate-parentheses';
2+
3+
describe('Generate Parentheses', () => {
4+
5+
test('n = 3', () => {
6+
const result = generateParentheses(3);
7+
const expected = [
8+
"((()))",
9+
"(()())",
10+
"(())()",
11+
"()(())",
12+
"()()()"
13+
];
14+
console.log(result);
15+
expect(result).toEqual(expected);
16+
});
17+
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import letterCombinationsOfAPhoneNumber from '../letter-combinations-of-a-phone-number';
2+
3+
describe('Letter Combinations of a Phone Number', () => {
4+
5+
test('23', () => {
6+
const result = letterCombinationsOfAPhoneNumber('23');
7+
const expected = [
8+
'ad',
9+
'ae',
10+
'af',
11+
'bd',
12+
'be',
13+
'bf',
14+
'cd',
15+
'ce',
16+
'cf',
17+
];
18+
expect(result).toEqual(expected);
19+
});
20+
21+
});
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import palindromePartitioning from '../palindrome-partitioning';
2+
3+
describe('Palindrome Partitioning', () => {
4+
test('aab', () => {
5+
const restult = palindromePartitioning('aab');
6+
const expected = [
7+
["a","a","b"],
8+
["aa","b"],
9+
];
10+
expect(restult).toEqual(expected);
11+
});
12+
});

__test__/permutations-ii.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import permutationsII from '../permutations-ii';
2+
3+
describe('Permutations II', () => {
4+
test('[1, 1, 2]', () => {
5+
const result = permutationsII([1, 1, 2]);
6+
const expected = [
7+
[1, 1, 2],
8+
[1, 2, 1],
9+
[2, 1, 1],
10+
];
11+
expect(result).toEqual(expected);
12+
});
13+
14+
test('[1]', () => {
15+
const result = permutationsII([1]);
16+
const expected = [
17+
[1],
18+
];
19+
expect(result).toEqual(expected);
20+
});
21+
22+
test('[1, 2]', () => {
23+
const result = permutationsII([1, 2]);
24+
const expected = [
25+
[1, 2],
26+
[2, 1],
27+
];
28+
expect(result).toEqual(expected);
29+
});
30+
31+
});

__test__/restore-ip-addresses.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import restoreIpAddresses from '../restore-ip-addresses';
2+
3+
describe('Restore IP Addresses', () => {
4+
5+
test('25525511135', () => {
6+
const result = restoreIpAddresses('25525511135');
7+
const expected = [
8+
'255.255.11.135',
9+
'255.255.111.35',
10+
];
11+
expect(result).toEqual(expected);
12+
});
13+
14+
});

__test__/subsets.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import subsets from '../subsets';
2+
3+
describe('Subsets', () => {
4+
test('[1, 2, 3]', () => {
5+
const result = subsets([1, 2, 3]);
6+
const expected = [
7+
[],
8+
[1],
9+
[1, 2],
10+
[1, 2, 3],
11+
[1, 3],
12+
[2],
13+
[2, 3],
14+
[3]
15+
];
16+
expect(result).toEqual(expected);
17+
});
18+
});

combination.js

-25
This file was deleted.

combinations-sum-iii.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
3+
*
4+
* Note:
5+
*
6+
* All numbers will be positive integers.
7+
* The solution set must not contain duplicate combinations.
8+
* Example 1:
9+
*
10+
* Input: k = 3, n = 7
11+
* Output: [[1,2,4]]
12+
* Example 2:
13+
*
14+
* Input: k = 3, n = 9
15+
* Output: [[1,2,6], [1,3,5], [2,3,4]]
16+
*/
17+
18+
/**
19+
* @param {number} count
20+
* @param {number} sum
21+
* @return {number[][]}
22+
*/
23+
const combinationSum3 = (count, sum) => {
24+
return getCombinations(count, sum);
25+
};
26+
27+
/**
28+
*
29+
* @param {number} count
30+
* @param {number} sum
31+
* @param {number} num
32+
* @param {number[]} temp
33+
* @param {number[][]} combinations
34+
*/
35+
const getCombinations = (count, sum, num = 1, temp = [], combinations = []) => {
36+
if (temp.length > count) {
37+
return;
38+
}
39+
40+
if (temp.length === count && sum === 0) {
41+
return combinations.push(temp.slice());
42+
}
43+
44+
for (let i = num; i <= 9; i++) {
45+
temp.push(i);
46+
getCombinations(count, sum - i, i + 1, temp, combinations);
47+
temp.pop();
48+
}
49+
50+
return combinations;
51+
};
52+
53+
export default combinationSum3;

combinations.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Combinations
3+
*
4+
* Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
5+
*
6+
* Example:
7+
*
8+
* Input: n = 4, k = 2
9+
* Output:
10+
* [
11+
* [2,4],
12+
* [3,4],
13+
* [2,3],
14+
* [1,2],
15+
* [1,3],
16+
* [1,4],
17+
* ]
18+
*/
19+
20+
const combinations = (n, k) => {
21+
return backtracking(n, k);
22+
};
23+
24+
const backtracking = (num, count, start = 1, list = [], result = []) => {
25+
26+
if (list.length > count) {
27+
return result;
28+
}
29+
30+
if (list.length === count) {
31+
return result.push(list.slice());
32+
}
33+
34+
for (let i = start; i <= num; i++) {
35+
list.push(i);
36+
backtracking(num, count, i + 1, list, result);
37+
list.pop();
38+
}
39+
40+
return result;
41+
};
42+
43+
// function combinations (m, n) {
44+
// const nums = [0, 1, 2, 3, 4];
45+
// const result = [];
46+
47+
// backtracking(nums, '', 0, 4);
48+
49+
// function backtracking(nums, str, start, len) {
50+
// if (str.length === 3) {
51+
// const t = str.split('').sort().join('');
52+
// if (result.indexOf(t) === -1) {
53+
// result.push(t);
54+
// }
55+
// }
56+
57+
// for (let i = start; i <= len; i++) {
58+
// backtracking(nums, str + nums[i], start + 1, len);
59+
// }
60+
61+
// }
62+
63+
64+
// return result;
65+
// }
66+
67+
console.log(combinations(4, 2));

generate-parentheses.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Generate Parentheses
3+
* Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
4+
*
5+
* For example, given n = 3, a solution set is:
6+
*
7+
* [
8+
* "((()))",
9+
* "(()())",
10+
* "(())()",
11+
* "()(())",
12+
* "()()()"
13+
* ]
14+
*/
15+
16+
const generateParentheses = n => {
17+
return generate(n, n);
18+
};
19+
20+
const generate = (left, right, parentheses = [], current = '') => {
21+
if (left === 0 && right === 0) {
22+
return parentheses.push(current);
23+
}
24+
25+
if (left > 0) {
26+
generate(left - 1, right, parentheses, current + '(');
27+
}
28+
29+
if (left < right) {
30+
generate(left, right - 1, parentheses, current + ')');
31+
}
32+
33+
return parentheses;
34+
};
35+
36+
export default generateParentheses;

0 commit comments

Comments
 (0)