[Leetcode 40]组合数和II Combination Sum II
【题目】
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
Each number in candidates may only be used once in the combination.
Note:
- All numbers (including
target) will be positive integers. - The solution set must not contain duplicate combinations.
- All numbers (including
Example 1:
Input: candidates =[10,1,2,7,6,1,5], target =8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]
【思路】
回溯,关键在去重:sort后nums[i-1]==nums[i] continue。
1、[Leetcode 78]求子集 Subset https://www.cnblogs.com/inku/p/9976049.html
2、[Leetcode 90]求含有重复数的子集 Subset II https://www.cnblogs.com/inku/p/9976099.html
3、讲解在这: [Leetcode 216]求给定和的数集合 Combination Sum III
4、[Leetcode 39]组合数的和Combination Sum
【代码】
有参考39的思路设置全局变量+两种情况合并。
class Solution {
private static List<List<Integer>> ans ;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
ans = new ArrayList<>();
fun(new ArrayList<Integer>(),candidates,target,0);
return ans;
}
public void fun(List<Integer> tmp,int[] data, int aim,int flag){
if(aim<=0){
if(aim==0){
ans.add(new ArrayList<>(tmp));
}
return;
}
for(int i=flag;i<data.length;i++){
if(i>flag&&data[i-1]==data[i])
continue;
tmp.add(data[i]);
fun(tmp,data,aim-data[i],i+1);
tmp.remove(tmp.size()-1);
}
}
}
[Leetcode 40]组合数和II Combination Sum II的更多相关文章
- [Leetcode 39]组合数的和Combination Sum
[题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...
- 【Leetcode】【Medium】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode每天一题】Combination Sum II(组合和II)
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [Swift]LeetCode40. 组合总和 II | Combination Sum II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [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)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
随机推荐
- SAP abap 内表增加字段方法,结构复用
1.include data: begin of gth_qamr. include structure qamr. data: kurztext like qamv-kurztext ...
- (Code) Python implementation of phrase extraction from sentence
import os import numpy as np import pandas as pd from tqdm import tqdm import numpy as np import str ...
- abap 常用 function
ABAP常用函数总结 alv .smartform. excel .text.邮件 .远程访问,FTP服务器... **********常用功能function REUSE_ALV_GRID_DI ...
- react native 项目使用 expo 二维码扫描失败
今天学习react native,需使用expo在移动端进行调试. npm start 运行项目后,使用expo扫描二维码,始终没有反应.于是决定采用这个方法: 连上手机打开usb调试后,按下‘a’, ...
- CentOS7 DHCP 服务搭建
一.实验环境 1.VMware12.俩台Linux(Ser 和 Client ).DHCP安装包. 二.操作流程 1.安装DHCP 2.配置DHCP的配置文件: /etc/dhcp/dhcpd. ...
- window上安装kafka(单机)
1.第一步骤,先安装JDK,请参考:https://www.cnblogs.com/xubao/p/10692861.html 2.第二步骤,安装zookeeper,请参考:https://www.c ...
- 图片裁剪 cropper.js 上传组件封装 vue
//HTML cropper.js 文档地址: https://github.com/fengyuanchen/cropperjs/blob/master/README.md <template ...
- shiro框架-配置
才开始学没有什么理解分享一个博客写的比较详细 借鉴大佬的:https://www.cnblogs.com/maofa/p/6407102.html@阿发仔 https://blog.csdn.net ...
- vue 双向绑定 数据修改但页面没刷新
在数据改动的代码后加 this.$forceUpdate(); 若是在某个特定方法中 则将this改为方法中设定的名称
- 从身份证管理系统思考企业CMDB的建设
关注嘉为科技,获取运维新知 对大部分中大型的企业来说,CMDB建设对于整个IT服务和IT运维管理的重要性不言而喻,但是目前仍然有非常多的企业无法建设好CMDB. 我最近刚好接触了一个公安系统的朋友,他 ...