【Leetcode】【Medium】Combination Sum 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]
解题思路:
求序列的排列组合,典型的回溯算法思想。
如果有兴趣查看Combination Sum版本I,请点击链接。
版本一中candidate没有重复,但是可以允许重复使用candidates中的元素;即,对于{1,2,3,4},选择了1,还可以继续在{1,2,3,4}中选择,返回序列不会出现重复结果;
版本二(本题)设置candidate有重复,但是每个candidate只能使用一次;即,对于{1,2,2,3,4},选择了1,只能继续在{2,2,3,4}中选择;
但是,因为candidate中“2”有重复,如果需要选择“2”,只能选择“第一个2”,不能选择“第二个2”;
因为如果target是10,选择第一个2会返回结果{1,2,3,4},但是选择第二个2,也会出现相同序列{1,2,3,4};造成返回队列包含重复结果;
选择“第一个2”后,还能继续从{2,3,4}中挑选下一个数,因此不会漏选;
代码:
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> lst;
vector<int> ans;
sort(candidates.begin(), candidates.end());
Backtracking(lst, ans, candidates, , target);
return lst;
} void Backtracking(vector<vector<int>> &lst, vector<int> ans, vector<int> candidates, int idx, int left) {
for (int i = idx; i < candidates.size(); ++i) {
if (i > idx && candidates[i] == candidates[i-])
continue;
int cur_v = left - candidates[i];
if (cur_v == ) {
ans.push_back(candidates[i]);
lst.push_back(ans);
return;
} if (cur_v > ) {
ans.push_back(candidates[i]);
Backtracking(lst, ans, candidates, i + , cur_v);
ans.pop_back();
} else {
break;
}
}
return;
}
};
【Leetcode】【Medium】Combination Sum II的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【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 ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- [Leetcode 39]组合数的和Combination Sum
[题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- 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) ...
随机推荐
- 安装 VMware Tools
参考帮助文档 错误#1: tar: vmware-tools-distrib: Cannot mkdir: Read-only file system 解决方法: mkdir /mnt/cdrom m ...
- android volley 发送 POST 请求
Map<String, String> params = new HashMap<String, String>(); params.put("fromUser&qu ...
- c++ 常用的遍历,删除,分割等等文件处理函数代码实现
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9622851.html 删除文件目录函数: void myDeleteDirectory(CSt ...
- 【随笔】Linux主机简单判断CC攻击的命令
今天看到一个很有意思的命令tcpdump,在这里记录下. 如果想要看tcpdump的详细用法,可以点击这里. 什么是CC攻击? 关于CC攻击,这里引用百度的解释: CC攻击的原理就是攻击者控制某些主机 ...
- jQuery插件的开发(一)
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- PHP中判断字符串是否包含某个字符时,建议使用正则表达式preg_match()
判断字符串中是否包含 某个字符时,在java中时直接使用 indexOf()来判断的 在php中好像也要对应的,strpos(),stripos() 不过每次我用的都很不爽,老是出现各种各样的小问题, ...
- Parcel Vs Webpack
横空出世的Parcel近日成为了前端圈的又一大热点,在短短几周内就获得了13K的Star.作为前端构建工具新人的Parcel为什么能在短期内获得这么多赞同?他和老大哥Webpack比起来到底有什么优势 ...
- js定义一个处理字符串的函数
//定义一个处理字符串的方法 function StringBuffer(str){ var arr = []; str = str || ''; arr.push(str); //追加字符串 thi ...
- golang学习之rpc实例
rpc(远程过程调用),可以像调用本地程序一样调用远端服务,rpc分为http方式和tcp连接方式,使用http的rpc调用如下: 首先是server端: // rpc_server project ...
- Cheatsheet: 2017 02.01 ~ 02.28
Web Debouncing and Throttling Explained Through Examples What is TypeScript? An Absolute Beginner's ...