zoukankan      html  css  js  c++  java
  • LeetCode OJ:Combination Sum II (组合之和 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.
    • 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.

    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] 

    题目实际和组合之和(见其他博文)很像,但是这里组合中的数是可以有重复的,但是每个数最多只能用一次,所以说实现上与前面那个有点不相似的地方,代码见下,注释写的还是比较清楚的:

     1 class Solution {
     2 public:
     3     vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
     4         tmpCdd = candidates;
     5         sort(tmpCdd.begin(), tmpCdd.end());
     6         vector<int> tmpVec;
     7         dfs(tmpVec, 0, target);//这个dfs的参数分别是当前vec中含有的元素数目
     8         return result;            //序号起始以及,距离target还差的数目
     9     }
    10 private:
    11     vector<int> tmpCdd;
    12     vector<vector<int>> result;
    13     void dfs(vector<int> & tmpVec, int index, int tgt)
    14     {
    15         if(tgt == 0){
    16             result.push_back(tmpVec);
    17             return;            //达到target,剪枝
    18         }
    19         if(index == tmpCdd.size()) return;
    20         else{
    21             for(int idx = index; idx < tmpCdd.size(); ++idx){
    22                 if (idx != index && tmpCdd[idx] == tmpCdd[idx - 1])
    23                     continue;    //这一步的主要目标是防止相邻相同的数和其他数一起匹配成为多个相同的vector,很关键。
    24                 if(tmpCdd[idx] <= tgt){
    25                     tmpVec.push_back(tmpCdd[idx]);    //这其他的实际上和combinationSum1是相同的
    26                     dfs(tmpVec, idx + 1, tgt - tmpCdd[idx]);
    27                     tmpVec.pop_back();
    28                 }
    29             }
    30         }
    31     }
    32 };

    大体就是这样,感觉写的有点乱,想想以后可能再来改。

    java版本如下所示,相比上面的写的条理相对的要清楚一点,代码如下所示:

     1 public class Solution {
     2     public List<List<Integer>> combinationSum2(int[] candidates, int target) {
     3         List<List<Integer>> ret = new ArrayList<List<Integer>>();
     4         Arrays.sort(candidates);
     5         for(int i = 0; i < candidates.length; ++i){
     6             List<Integer> curr = new ArrayList<Integer>();
     7             if(i != 0 && candidates[i] == candidates[i-1])   //注意这里和下面同样的地方,防止出现相同的组合
     8                 continue;
     9             curr.add(candidates[i]);
    10             getCombination(ret, i + 1, target - candidates[i], curr, candidates);
    11             curr.remove(curr.size() - 1);
    12         }
    13         return ret;
    14     }
    15     public void getCombination(List<List<Integer>> ret, int index, int target, List<Integer> tmpCdd, int [] candidates){
    16         if(index > candidates.length)
    17             return;
    18         if(target < 0){
    19             return;
    20         }else if(target == 0){
    21             ret.add(new ArrayList<Integer>(tmpCdd));
    22             return;
    23         }else{
    24             for(int i = index; i < candidates.length; ++i){
    25                 if(i != index && candidates[i] == candidates[i-1])
    26                     continue;
    27                 tmpCdd.add(candidates[i]);
    28                 getCombination(ret, i + 1, target - candidates[i], tmpCdd, candidates);
    29                 tmpCdd.remove(tmpCdd.size() - 1);
    30             }
    31         }
    32     }
    33 }
  • 相关阅读:
    python 查看安装包列表
    Centos7 linux下通过源码安装redis以及使用
    python 安装pip
    python3.7.2 pip 出现locations that require TLS/SSL异常处理方法
    Xshell报错“The remote SSH server rejected X11 forwarding request.”
    ERROR: child process failed, exited with error number 100
    centos7 (ifconfig不能使用) -bash: ifconfig: command not found
    gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now
    Centos7 安装python3.7.2
    Python安装常见问题:zipimport.ZipImportError: can't decompress data; zlib not available 解决办法
  • 原文地址:https://www.cnblogs.com/-wang-cheng/p/4866762.html
Copyright © 2011-2022 走看看