Combination Sum 1
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7]
and target 7
,
A solution set is:
[ [7], [2, 2, 3] ]
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates);
backtrack(res, new ArrayList<Integer>(), candidates, target, 0);
return res;
}
private void backtrack(List<List<Integer>> res, List<Integer> tmp, int[] arr, int remain, int pos) {
if (remain < 0) return;
else if (remain == 0) res.add(new ArrayList<Integer>(tmp));
else {
for (int i = pos; i < arr.length; i++) {
tmp.add(arr[i]);
backtrack(res, tmp, arr, remain - arr[i], i);
tmp.remove(tmp.size() - 1);
}
}
}
Combination Sum 2
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5]
and target 8
,
A solution set is:
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(candidates);
backtrack(list, new ArrayList<Integer>(), candidates, target, 0);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> temp, int[] arr, int remain, int start) {
if (remain < 0) return;
else if (remain == 0) list.add(new ArrayList<Integer>(temp));
else {
for (int i = start; i < arr.length; i ++) {
if (i > start && arr[i] == arr[i - 1]) continue;
temp.add(arr[i]);
backtrack(list, temp, arr, remain - arr[i], i + 1);
temp.remove(temp.size() - 1);
}
}
}
Combination Sum 3
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.
Example 1:
Input: k = 3, n = 7
Output:
[[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<Integer>(), k, n, 1);
return res;
}
private void backtrack(List<List<Integer>> res, List<Integer> tmp, int k, int remain, int start) {
if (remain < 0) return;
else if (remain == 0 && tmp.size() == k) res.add(new ArrayList<Integer>(tmp));
else {
for (int i = start; i <= 9; i++) {
tmp.add(i);
backtrack(res, tmp, k, remain - i, i + 1);
tmp.remove(tmp.size() - 1);
}
}
}
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<Integer>(), 1, n, k);
return res;
}
private void backtrack(List<List<Integer>> res, List<Integer> tmp, int start, int n, int k) {
if (k == 0) res.add(new ArrayList<Integer>(tmp));
for (int i = start; i <= n; i++) {
tmp.add(i);
backtrack(res, tmp, i + 1, n, k - 1);
tmp.remove(tmp.size() - 1);
}
}
Permutation
Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums);
return res;
}
private void backtrack(List<List<Integer>> res, List<Integer> tmp, int[] nums) {
if (tmp.size() == nums.length) {
res.add(new ArrayList<>(tmp));
return;
}
for (int i = 0; i < nums.length; i++) {
if (tmp.contains(nums[i])) continue;
tmp.add(nums[i]);
backtrack(res, tmp, nums);
tmp.remove(tmp.size() - 1);
}
}
Factor Combinations
Numbers can be regarded as product of its factors. For example,
8 = 2 x 2 x 2; = 2 x 4.
Write a function that takes an integer n and return all possible combinations of its factors.
Note:
- You may assume that n is always positive.
- Factors should be greater than 1 and less than n.
Examples:
input: 1
output:
[]input:
37
output:
[]input:
12
output:
[ [2, 6], [2, 2, 3], [3, 4] ]input:
32
output:
[ [2, 16], [2, 2, 8], [2, 2, 2, 4], [2, 2, 2, 2, 2], [2, 4, 4], [4, 8] ]
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> res = new ArrayList<List<Integer>> ();
helper(res, new ArrayList<Integer> (), n, 2);
return res;
}
private void helper(List<List<Integer>> res, List<Integer> tmp, int n, int start) {
if (n == 1 && tmp.size() > 1) {
res.add(new ArrayList<Integer>(tmp));
return;
}
for (int i = start; i <= n; i++) {
if (n % i == 0) {
tmp.add(i);
helper(res, tmp, n / i, i);
tmp.remove(tmp.size() - 1);
}
}
}
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
nums = [1, 2, 3] target = 4 The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7.
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target + 1];
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int num : nums) {
if (i >= num) dp[i] += dp[i - num];
}
}
return dp[target];
}