vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
set<string> seen;
vector<vector<int>> result;
vector<int> path;
function<void(int)> dfs = [&](int sum) {
if (sum > target) return;
if (sum == target) {
vector<int> combo = path; sort(combo.begin(), combo.end());
string key; for (int x : combo) key += to_string(x) + ",";
if (!seen.count(key)) {
seen.insert(key);
result.push_back(combo);
}
return;
}
for (int c : candidates) {
path.push_back(c);
dfs(sum + c);
path.pop_back();
}
};
dfs(0);
return result;
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Set<String> seen = new HashSet<>();
List<List<Integer>> result = new ArrayList<>();
dfs(candidates, target, new ArrayList<>(), 0, seen, result);
return result;
}
private void dfs(int[] candidates, int target, List<Integer> path, int sum, Set<String> seen, List<List<Integer>> result) {
if (sum > target) return;
if (sum == target) {
List<Integer> combo = new ArrayList<>(path);
Collections.sort(combo);
String key = combo.toString();
if (!seen.contains(key)) {
seen.add(key);
result.add(combo);
}
return;
}
for (int c : candidates) {
path.add(c);
dfs(candidates, target, path, sum + c, seen, result);
path.remove(path.size() - 1);
}
}
def combination_sum(candidates, target):
seen = set()
result = []
def dfs(path, total):
if total > target: return
if total == target:
combo = sorted(path)
key = tuple(combo)
if key not in seen:
seen.add(key)
result.append(combo)
return
for c in candidates:
path.append(c)
dfs(path, total + c)
path.pop()
dfs([], 0)
return result
function combinationSum(candidates: number[], target: number): number[][] {
const seen = new Set<string>();
const result: number[][] = [];
const dfs = (path: number[], sum: number): void => {
if (sum > target) return;
if (sum === target) {
const combo = [...path].sort((a, b) => a - b);
const key = combo.join(',');
if (!seen.has(key)) {
seen.add(key);
result.push(combo);
}
return;
}
for (const c of candidates) {
path.push(c);
dfs(path, sum + c);
path.pop();
}
};
dfs([], 0);
return result;
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> result;
vector<int> path;
function<void(int, int)> dfs = [&](int start, int sum) {
if (sum > target) return;
if (sum == target) {
result.push_back(path);
return;
}
for (int i = start; i < (int)candidates.size(); i++) {
path.push_back(candidates[i]);
dfs(i, sum + candidates[i]);
path.pop_back();
}
};
dfs(0, 0);
return result;
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
dfs(candidates, target, 0, 0, new ArrayList<>(), result);
return result;
}
private void dfs(int[] candidates, int target, int start, int sum, List<Integer> path, List<List<Integer>> result) {
if (sum > target) return;
if (sum == target) {
result.add(new ArrayList<>(path));
return;
}
for (int i = start; i < candidates.length; i++) {
path.add(candidates[i]);
dfs(candidates, target, i, sum + candidates[i], path, result);
path.remove(path.size() - 1);
}
}
def combination_sum(candidates, target):
result = []
path = []
def dfs(start, total):
if total > target: return
if total == target:
result.append(path[:])
return
for i in range(start, len(candidates)):
path.append(candidates[i])
dfs(i, total + candidates[i])
path.pop()
dfs(0, 0)
return result
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = [];
const path: number[] = [];
const dfs = (start: number, sum: number): void => {
if (sum > target) return;
if (sum === target) {
result.push([...path]);
return;
}
for (let i = start; i < candidates.length; i++) {
path.push(candidates[i]);
dfs(i, sum + candidates[i]);
path.pop();
}
};
dfs(0, 0);
return result;
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<int> sorted = candidates; sort(sorted.begin(), sorted.end());
vector<vector<int>> result;
vector<int> path;
function<void(int, int)> dfs = [&](int start, int remain) {
if (remain == 0) {
result.push_back(path);
return;
}
for (int i = start; i < (int)sorted.size(); i++) {
if (sorted[i] > remain) break;
path.push_back(sorted[i]);
dfs(i, remain - sorted[i]);
path.pop_back();
}
};
dfs(0, target);
return result;
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
int[] sorted = candidates.clone(); Arrays.sort(sorted);
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
dfs(sorted, 0, target, path, result);
return result;
}
private void dfs(int[] sorted, int start, int remain, List<Integer> path, List<List<Integer>> result) {
if (remain == 0) {
result.add(new ArrayList<>(path));
return;
}
for (int i = start; i < sorted.length; i++) {
if (sorted[i] > remain) break;
path.add(sorted[i]);
dfs(sorted, i, remain - sorted[i], path, result);
path.remove(path.size() - 1);
}
}
def combination_sum(candidates, target):
sorted_c = sorted(candidates)
result = []
path = []
def dfs(start, remain):
if remain == 0:
result.append(path[:])
return
for i in range(start, len(sorted_c)):
if sorted_c[i] > remain: break
path.append(sorted_c[i])
dfs(i, remain - sorted_c[i])
path.pop()
dfs(0, target)
return result
function combinationSum(candidates: number[], target: number): number[][] {
const sorted = [...candidates].sort((a, b) => a - b);
const result: number[][] = [];
const path: number[] = [];
const dfs = (start: number, remain: number): void => {
if (remain === 0) {
result.push([...path]);
return;
}
for (let i = start; i < sorted.length; i++) {
if (sorted[i] > remain) break;
path.push(sorted[i]);
dfs(i, remain - sorted[i]);
path.pop();
}
};
dfs(0, target);
return result;
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<vector<int>>> table(target + 1);
table[0].push_back({});
for (int c : candidates) {
for (int t = c; t <= target; t++) {
for (const auto& combo : table[t - c]) {
vector<int> next = combo; next.push_back(c); table[t].push_back(next);
}
}
}
return table[target];
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<List<Integer>>> table = new ArrayList<>();
for (int t = 0; t <= target; t++) table.add(new ArrayList<>());
table.get(0).add(new ArrayList<>());
for (int c : candidates) {
for (int t = c; t <= target; t++) {
for (List<Integer> combo : table.get(t - c)) {
List<Integer> next = new ArrayList<>(combo);
next.add(c);
table.get(t).add(next);
}
}
}
return table.get(target);
}
def combination_sum(candidates, target):
table = [[] for _ in range(target + 1)]
table[0].append([])
for c in candidates:
for t in range(c, target + 1):
for combo in table[t - c]: table[t].append(combo + [c])
return table[target]
function combinationSum(candidates: number[], target: number): number[][] {
const table: number[][][] = Array.from({ length: target + 1 }, () => []);
table[0].push([]);
for (const c of candidates) {
for (let t = c; t <= target; t++) {
for (const combo of table[t - c]) table[t].push([...combo, c]);
}
}
return table[target];
}