Problem description: http://oj.leetcode.com/problems/combination-sum-ii/

Basic idea: use recursive approach, remember to avoid duplicate item.

 class Solution {
public:
bool isExist(vector<vector<int> > &combinations, vector<int> &item) {
for(auto com: combinations){
if(com.size() == item.size()){
int k;
for(k = ; k < com.size(); k++){
if(com[k] != item[k])
break;
}
if(k == com.size())
return true;
}
}
return false;
}
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<vector<int> > combinations;
std::sort(num.begin(), num.end());
int is_target_equal_to_num = ;
for(int i = ; i < num.size(); i ++){
if(num[i] == target && !is_target_equal_to_num){
is_target_equal_to_num = ;
vector<int> combination(, target);
combinations.push_back(combination);
}else if(num[i] > target) {
break;
}else {
vector<int> sub_num(num.begin() + i + , num.end());
vector<vector<int> > sub_combinations = combinationSum2(sub_num, target - num[i]);
for (auto item : sub_combinations) {
item.insert(item.begin(), num[i]);
if(!isExist(combinations, item))
combinations.push_back(item);
}
}
} return combinations;
}
};

Combination Sum II [LeetCode]的更多相关文章

  1. Combination Sum II leetcode java

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  2. Combination Sum II —— LeetCode

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  4. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  5. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  6. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  7. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  8. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  9. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

随机推荐

  1. [dataTables.js error] Uncaught TypeError: myTable.row is not a function

    使用dataTables.js时遇到的问题. 代码如下: var myTable = $('#dynamic-table') .dataTable({ bAutoWidth : false, &quo ...

  2. [C和指针]第一部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. C语言程序设计现代方法_基本类型(第七章)

    C语言支持两种不同的数值类型,整数类型,浮点类型. C语言的整数类型有不同的尺寸.int类型通常为32位,但在老的CPU上可能是16位.有些可能是64位. 因此,int型如果在16位CPU上最大值就是 ...

  4. FZU 2214 Knapsack problem(背包问题)

    Description 题目描述 Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...

  5. So easy Webservice 2.WebService介绍

    WebService概念介绍: Web Service是一项新技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实 ...

  6. Codeforces Round #377 (Div. 2) D. Exams 二分

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  7. Linux基础※※※※如何使用Git in Linux(一)

    参考资料: 1. https://www.linux.com/learn/tutorials/796387-beginning-git-and-github-for-linux-users/ 2. h ...

  8. Android实现简单短信发送器

    布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  9. maven2 配置M2_REPO

    从eclipse中增加了maven2的插件之后,maven默认的本地库的路径是${user}/.m2/repository/下,一般windows用户的操作系统都安装在C盘,所以这个目录 下的jar包 ...

  10. 将客户端将IE9强制为IE7

    有时候由于浏览器的问题我们在IE7中开发的东西需要在IE9中展示 但是会出现兼容性的问题. 那么我们可以同技巧将用户端的浏览器强行以IE7的文档模式展示我们的网页 下面是针对iis asp.net程序 ...