<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Combination Sum ### Source - leetcode: [Combination Sum | LeetCode OJ](https://leetcode.com/problems/combination-sum/) - lintcode: [(135) Combination Sum](http://www.lintcode.com/en/problem/combination-sum/) ~~~ Given a set of candidate numbers (C) 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. For example, given candidate set 2,3,6,7 and target 7, A solution set is: [7] [2, 2, 3] Have you met this question in a real interview? Yes Example given candidate set 2,3,6,7 and target 7, A solution set is: [7] [2, 2, 3] Note - All numbers (including target) will be positive integers. - Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak). - The solution set must not contain duplicate combinations. ~~~ ### 題解 和 [Permutations](http://algorithm.yuanbin.me/zh-cn/exhaustive_search/permutations.html) 十分類似,區別在于剪枝函數不同。這里允許一個元素被多次使用,故遞歸時傳入的索引值不自增,而是由 for 循環改變。 ### Java ~~~ public class Solution { /** * @param candidates: A list of integers * @param target:An integer * @return: A list of lists of integers */ public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> result = new ArrayList<List<Integer>>(); List<Integer> list = new ArrayList<Integer>(); if (candidates == null) return result; Arrays.sort(candidates); helper(candidates, 0, target, list, result); return result; } private void helper(int[] candidates, int pos, int gap, List<Integer> list, List<List<Integer>> result) { if (gap == 0) { // add new object for result result.add(new ArrayList<Integer>(list)); return; } for (int i = pos; i < candidates.length; i++) { // cut invalid candidate if (gap < candidates[i]) { return; } list.add(candidates[i]); helper(candidates, i, gap - candidates[i], list, result); list.remove(list.size() - 1); } } } ~~~ ### 源碼分析 對數組首先進行排序是必須的,遞歸函數中本應該傳入 target 作為入口參數,這里借用了 Soulmachine 的實現,使用 gap 更容易理解。注意在將臨時 list 添加至 result 中時需要 new 一個新的對象。 ### 復雜度分析 按狀態數進行分析,時間復雜度 O(n!)O(n!)O(n!), 使用了list 保存中間結果,空間復雜度 O(n)O(n)O(n). ### Reference - Soulmachine 的 leetcode 題解
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看