1. 程式人生 > 其它 >Leetcode216/39/40/77之回溯解決經典組合問題

Leetcode216/39/40/77之回溯解決經典組合問題

Leetcode216-組合總和三

  • 找出所有相加之和為 n 的 k 個數的組合,且滿足下列條件:
  • 只使用數字1到9
  • 每個數字 最多使用一次
  • 返回 所有可能的有效組合的列表 。該列表不能包含相同的組合兩次,組合可以以任何順序返回
  • 輸入: k = 3, n = 7
  • 輸出: [[1,2,4]]
  ArrayList<List<Integer>> res=new ArrayList<>();
    LinkedList<Integer> integers=new LinkedList<>();
    int sum=0;
    public List<List<Integer>> combinationSum3(int k, int n) {
        combine(k,n,1);
        return res;
    }


    public void combine(int k,int n,int startIndex) {
        if (sum != n && integers.size() == k) {
            return;
        }
        if (sum == n && integers.size() == k) {
            res.add(new LinkedList<Integer>(integers));
        }
        for (int i = startIndex; i <= 9; i++) {
            sum += i;
            integers.addFirst(i);
            //使用addFirst和removeFirst每個integers裡面是倒序的
            //要正序使用add和removeLast就行了
            combine(k, n, i + 1);
            integers.removeFirst();
            sum -= i;
        }
    }

Leetcode39-組合總和

  • 給你一個 無重複元素 的整數陣列 candidates 和一個目標整數 target ,找出 candidates 中可以使數字和為目標數 target 的 所有 不同組合 ,並以列表形式返回。你可以按 任意順序 返回這些組合。
  • candidates 中的 同一個 數字可以 無限制重複被選取 。如果至少一個數字的被選數量不同,則兩種組合是不同的。
  • 輸入:candidates = [2,3,6,7], target = 7
  • 輸出:[[2,2,3],[7]]
    ArrayList<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> integers = new LinkedList<>();
    int sum = 0;

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        combine(candidates, target, 0);
        return res;
    }

    public void combine(int[] candidates, int target, int startIndex) {
        if (sum > target) {
            return;
        }
        if (sum == target) {
            res.add(new LinkedList<>(integers));
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            sum += candidates[i];
            integers.add(candidates[i]);
            combine(candidates, target, i);
            integers.removeLast();
            sum -= candidates[i];
        }
    }

Leetcode40-組合總和二

  • 給定一個候選人編號的集合 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。
  • candidates 中的每個數字在每個組合中只能使用 一次
  • 注意:解集不能包含重複的組合。
  • 輸入: candidates = [10,1,2,7,6,1,5], target = 8,
  • 輸出:
    [
    [1,1,6],
    [1,2,5],
    [1,7],
    [2,6]
    ]
  ArrayList<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> integers = new LinkedList<>();
    int sum = 0;

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        //加標誌陣列,用來輔助判斷同層節點是否已經遍歷
        boolean[] flag = new boolean[candidates.length];
        combine(candidates, target, 0, flag);
        return res;
    }

    public void combine(int[] candidates, int target, int startIndex, boolean[] flag) {
        if (sum > target) {
            return;
        }
        if (sum == target) {
            res.add(new LinkedList<>(integers));
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            //出現重複節點直接跳過
            if (i > 0 && candidates[i] == candidates[i - 1] && flag[i - 1] == false) {
                continue;
            }
            flag[i] = true;
            sum += candidates[i];
            integers.add(candidates[i]);
            combine(candidates, target, i + 1, flag);
            integers.removeLast();
            sum -= candidates[i];
            flag[i] = false;
        }
    }

L77-組合

  • 給定兩個整數 n 和 k,返回範圍 [1, n] 中所有可能的 k 個數的組合。
  • 你可以按 任何順序 返回答案
  • 輸入:n = 4, k = 2
  • 輸出:
    [
    [2,4],
    [3,4],
    [2,3],
    [1,2],
    [1,3],
    [1,4],
    ]
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> combine(int n, int k) {
        combineRes(n,k,1);
        return result;
    }

    public void combineRes(int n,int k,int startIndex){
        //終止條件
        if(path.size()==k){
            result.add(new ArrayList<>(path));//防止遞迴操作影響以及儲存好的path
            return;
        }
        //剪枝優化 此處仔細想想
        //如果for迴圈選擇的起始位置之後的元素個數 已經不足 我們需要的元素個數了,那麼就沒有必要搜尋了。
        //已經選擇的元素個數:path.size();
        //還需要的元素個數為: k - path.size();
        //在集合n中至多要從該起始位置 : n - (k - path.size()) + 1,開始遍歷
        //不剪枝直接 for(int i=startIndex;i<=n;i++)
        for(int i=startIndex;i<=n-(k-path.size())+1;i++){
            path.addFirst(i);
            combineRes(n,k,i+1);
            path.removeFirst();
        }
    }