<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 功能強大 支持多語言、二開方便! 廣告
                # 0040. 組合總和 II ## 題目地址(40. 組合總和 II) <https://leetcode-cn.com/problems/combination-sum-ii/> ## 題目描述 ``` <pre class="calibre18">``` 給定一個數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的每個數字在每個組合中只能使用一次。 說明: 所有數字(包括目標數)都是正整數。 解集不能包含重復的組合。 示例 1: 輸入: candidates = [10,1,2,7,6,1,5], target = 8, 所求解集為: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] 示例 2: 輸入: candidates = [2,5,2,1,2], target = 5, 所求解集為: [ [1,2,2], [5] ] ``` ``` ## 前置知識 - 回溯法 ## 公司 - 阿里 - 騰訊 - 百度 - 字節 ## 思路 這道題目是求集合,并不是`求極值`,因此動態規劃不是特別切合,因此我們需要考慮別的方法。 這種題目其實有一個通用的解法,就是回溯法。 網上也有大神給出了這種回溯法解題的 [通用寫法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),這里的所有的解法使用通用方法解答。 除了這道題目還有很多其他題目可以用這種通用解法,具體的題目見后方相關題目部分。 我們先來看下通用解法的解題思路,我畫了一張圖: ![](https://img.kancloud.cn/46/ec/46ec3382e572355e2109c47284e37e4b_1341x1080.jpg) > 圖是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,僅做參考。 通用寫法的具體代碼見下方代碼區。 ## 關鍵點解析 - 回溯法 - backtrack 解題公式 ## 代碼 - 語言支持: Javascript,Python3 ``` <pre class="calibre18">``` <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">backtrack</span>(<span class="hljs-params">list, tempList, nums, remain, start</span>) </span>{ <span class="hljs-keyword">if</span> (remain < <span class="hljs-params">0</span>) <span class="hljs-keyword">return</span>; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (remain === <span class="hljs-params">0</span>) <span class="hljs-keyword">return</span> list.push([...tempList]); <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = start; i < nums.length; i++) { <span class="hljs-title">// 和39.combination-sum 的其中一個區別就是這道題candidates可能有重復</span> <span class="hljs-title">// 代碼表示就是下面這一行</span> <span class="hljs-keyword">if</span>(i > start && nums[i] == nums[i<span class="hljs-params">-1</span>]) <span class="hljs-keyword">continue</span>; <span class="hljs-title">// skip duplicates</span> tempList.push(nums[i]); backtrack(list, tempList, nums, remain - nums[i], i + <span class="hljs-params">1</span>); <span class="hljs-title">// i + 1代表不可以重復利用, i 代表數字可以重復使用 </span> tempList.pop(); } } <span class="hljs-title">/** * @param {number[]} candidates * @param {number} target * @return {number[][]} */</span> <span class="hljs-keyword">var</span> combinationSum2 = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">candidates, target</span>) </span>{ <span class="hljs-keyword">const</span> list = []; backtrack(list, [], candidates.sort((a, b) => a - b), target, <span class="hljs-params">0</span>); <span class="hljs-keyword">return</span> list; }; ``` ``` Python3 Code: ``` <pre class="calibre18">``` <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">combinationSum2</span><span class="hljs-params">(self, candidates: List[int], target: int)</span> -> List[List[int]]:</span> <span class="hljs-string">""" 與39題的區別是不能重用元素,而元素可能有重復; 不能重用好解決,回溯的index往下一個就行; 元素可能有重復,就讓結果的去重麻煩一些; """</span> size = len(candidates) <span class="hljs-keyword">if</span> size == <span class="hljs-params">0</span>: <span class="hljs-keyword">return</span> [] <span class="hljs-title"># 還是先排序,主要是方便去重</span> candidates.sort() path = [] res = [] self._find_path(candidates, path, res, target, <span class="hljs-params">0</span>, size) <span class="hljs-keyword">return</span> res <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">_find_path</span><span class="hljs-params">(self, candidates, path, res, target, begin, size)</span>:</span> <span class="hljs-keyword">if</span> target == <span class="hljs-params">0</span>: res.append(path.copy()) <span class="hljs-keyword">else</span>: <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(begin, size): left_num = target - candidates[i] <span class="hljs-keyword">if</span> left_num < <span class="hljs-params">0</span>: <span class="hljs-keyword">break</span> <span class="hljs-title"># 如果存在重復的元素,前一個元素已經遍歷了后一個元素與之后元素組合的所有可能</span> <span class="hljs-keyword">if</span> i > begin <span class="hljs-keyword">and</span> candidates[i] == candidates[i<span class="hljs-params">-1</span>]: <span class="hljs-keyword">continue</span> path.append(candidates[i]) <span class="hljs-title"># 開始的 index 往后移了一格</span> self._find_path(candidates, path, res, left_num, i+<span class="hljs-params">1</span>, size) path.pop() ``` ``` ## 相關題目 - [39.combination-sum](39.combination-sum.html) - [46.permutations](46.permutations.html) - [47.permutations-ii](47.permutations-ii.html) - [78.subsets](78.subsets.html) - [90.subsets-ii](90.subsets-ii.html) - [113.path-sum-ii](113.path-sum-ii.html) - [131.palindrome-partitioning](131.palindrome-partitioning.html)
                  <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>

                              哎呀哎呀视频在线观看