LeetCode-Combination Sum

来源:互联网 发布:淘宝恶意举报假货 编辑:程序博客网 时间:2024/05/24 00:53

39. Combination Sum

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]]
import java.util.*;/** * 39. Combination Sum */class Solution {    private List<Integer> cur;    private List<List<Integer>> result;    private void DFS(int[] arr, int target, int pos) {        if (target < 0) {            return;        }        if (0 == target) {            result.add(new ArrayList<>(cur));            return;        }        for (int i = pos; i < arr.length; i++) {            cur.add(arr[i]);            DFS(arr,target - arr[i], i);            cur.remove(cur.size() - 1);        }    }    public List<List<Integer>> combinationSum(int[] candidates, int target) {        cur = new LinkedList<>();        result = new LinkedList<>();        Arrays.sort(candidates);        DFS(candidates, target, 0);        return result;    }}

40. Combination Sum II

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]]

DFS+Backtracking的题目,但是搜索过程中需要剪枝。
对于如上的序列,我们先对它进行排序1 1 2 5 6 7 10
当以第二个1 为搜索起点时,当前一个元素也为1 并且没有被访问过,则这样一定构成一个重复访问序列。

package solutions._40;import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * 40. Combination Sum II */class Solution {    private boolean[] visited;    private List<List<Integer>> result;    private List<Integer> cur;    private void DFS(int[] arr, int target, int pos, int sum) {        if (sum > target) {            return;        }        if (target == sum) {            result.add(new ArrayList<>(cur));            return;        }        for (int i = pos; i < arr.length; i++) {            if (visited[i]) {                continue;            }            if (i != 0 && arr[i] == arr[i - 1] && !visited[i - 1]) {                continue;            }            if (!visited[i]) {                visited[i] = true;                cur.add(arr[i]);                DFS(arr, target, i, sum + arr[i]);                visited[i] = false;                cur.remove(cur.size() - 1);            }        }    }    public List<List<Integer>> combinationSum2(int[] candidates, int target) {        Arrays.sort(candidates);        visited = new boolean[candidates.length];        result = new ArrayList<>();        cur = new ArrayList<>();        DFS(candidates, target, 0, 0);        return result;    }    public static void main(String[] args) {        Solution solution = new Solution();        int[] arr = {10, 1, 2, 7, 6, 1, 5};        List<List<Integer>> list = solution.combinationSum2(arr, 8);        System.out.println(list);    }}

216. Combination Sum III

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]]
package solutions._216;import java.util.ArrayList;import java.util.List;/** * 216. Combination Sum III */class Solution {    private boolean[] visited;    private List<List<Integer>> result;    private List<Integer> cur;    private void DFS(int k, int sum, int n, int pos) {        if (cur.size() > k || sum > n) {            return;        }        if (k == cur.size() && sum == n) {            result.add(new ArrayList<>(cur));            return;        }        for (int i = pos + 1; i <= 9; i++) {            cur.add(i);            DFS(k, sum + i, n, i);            cur.remove(cur.size() - 1);        }    }    public List<List<Integer>> combinationSum3(int k, int n) {        visited = new boolean[n + 1];        result = new ArrayList<>();        cur = new ArrayList<>();        DFS(k, 0, n, 0);        return result;    }    public static void main(String[] args) {        Solution solution = new Solution();        List<List<Integer>> list = solution.combinationSum3(3, 9);        System.out.println(list);    }}
原创粉丝点击