LeetCode 040 Combination Sum II
题目要求: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 里面的元素可以无限次使用,但是Combination Sum II每个元素只能使用一次。
代码如下:
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(candidates.begin(), candidates.end());
set<vector<int> > ans;
vector<int> record;
searchAns(ans, record, candidates, target, 0);
vector<vector<int> > temp;
for (set<vector<int> >::iterator it = ans.begin(); it != ans.end(); it++) {
temp.push_back(*it);
}
return temp;
}
private:
void searchAns(set<vector<int> > &ans, vector<int> &record, vector<int> &candidates, int target, int idx) {
if (target == 0) {
ans.insert(record);
return;
}
if (idx == candidates.size() || candidates[idx] > target) {
return;
}
for (int i = 1; i >= 0; i--) {
record.push_back(candidates[idx]);
}
for (int i = 1; i >= 0; i--) {
record.pop_back();
searchAns(ans, record, candidates, target - i * candidates[idx], idx + 1);
}
}
};
LeetCode 040 Combination Sum II的更多相关文章
- Java for LeetCode 040 Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- 【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组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- kube-proxy实现原理
1.service概念 service是一组pod的服务抽象,相当于一组pod的LB,负责将请求分发给对应的pod.service会为这个LB提供一个IP,一般称为cluster IP.kube-pr ...
- 部署Dotnet Core应用到Kubernetes(一)
最近闲了点,写个大活:部署Dotnet应用到K8s. 写在前边的话 一直想完成这个主题.但这个主题实在太大了,各种拖延症的小宇宙不时爆发一下,结果就拖到了现在. 这个主题,会是一个系列.在这个 ...
- (5)ASP.NET Core3.1 Ocelot服务质量
1.服务质量(Quality of Service) 对于微服务来说,熔断就是我们常说的"保险丝",意思是当服务出现某些状况时候,通过切断服务防止应用程序不断地执行可能会失败的操作 ...
- Chrome默认启动尺寸的小问题
记录一个小问题. 这是我的Chrome,他默认启动的时候是这样的: 默认启动的尺寸似乎不可调,网上没有相关资料,简直让强迫症患者无所适从,这里记录一下偶然发现的方法: 调整Chrome的尺寸,选择右上 ...
- laravel 控制器中获取不到session
protected $middleware = [ \Illuminate\Session\Middleware\StartSession::class, ]; 在 kernel.php中 加入Sta ...
- 深度探秘.NET 5.0
今年11月10号 .NET 5.0 如约而至.这是.NET All in one后的第一个版本,虽然不是LTS(Long term support)版本,但是是生产环境可用的. 有微软的背书,微软从. ...
- axios封装接口
我们一般都是在做一个大型项目的时候,需要用到很多接口时,我们为了方便使用,就把接口封装起来. 先安装axios命令 :npm install axios --save 那么思路是什么呢? 首先在src ...
- ESP32的Linux开发环境搭建
1. 官网教程地址 https://docs.espressif.com/projects/esp-idf/zh_CN/v4.0.1/get-started/linux-setup.html 2.官网 ...
- waf 引擎云原生调研---扫盲
概念: lstio Istio是一个用于服务治理的开放平台 Istio是一个Service Mesh形态的用于服务治理的开放平台 Istio是一个与Kubernetes紧密结合的适用于云原生场景的Se ...
- Linux (操作二)
1.U盘的装载与卸载(设备都保存在/dev中 /dev存放设备的文件) 1.卸载u盘 umount /media/xxx/xxx (xxx为具体路径) 2.查看设备 sudo fdisk -l ( ...