[LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-40-Combination-Sum-II.html
描述
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.
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]
]
解析
回溯法
和上一道题非常像了,区别在于这里给的数组中有重复的数字,每个数字只能使用一次,然后同样是给出所有和等于 target 的情况。
代码
回溯法
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
getAnswer(ans, new ArrayList<>(), candidates, target, 0);
/*************修改的地方*******************/
// 如果是 Input: candidates = [2,5,2,1,2], target = 5,
// 输出会出现 [2 2 1] [2 1 2] 这样的情况,所以要去重
return removeDuplicate(ans);
/****************************************/
} private void getAnswer(List<List<Integer>> ans, ArrayList<Integer> temp, int[] candidates, int target, int start) {
if (target == 0) {
ans.add(new ArrayList<Integer>(temp));
} else if (target < 0) {
return;
} else {
for (int i = start; i < candidates.length; i++) {
temp.add(candidates[i]);
/*************修改的地方*******************/
//i -> i + 1 ,因为每个数字只能用一次,所以下次遍历的时候不从自己开始
getAnswer(ans, temp, candidates, target - candidates[i], i + 1);
/****************************************/
temp.remove(temp.size() - 1);
}
} } private List<List<Integer>> removeDuplicate(List<List<Integer>> list) {
Map<String, String> ans = new HashMap<String, String>();
for (int i = 0; i < list.size(); i++) {
List<Integer> l = list.get(i);
Collections.sort(l);
String key = "";
for (int j = 0; j < l.size() - 1; j++) {
key = key + l.get(j) + ",";
}
key = key + l.get(l.size() - 1);
ans.put(key, "");
}
List<List<Integer>> ans_list = new ArrayList<List<Integer>>();
for (String k : ans.keySet()) {
String[] l = k.split(",");
List<Integer> temp = new ArrayList<Integer>();
for (int i = 0; i < l.length; i++) {
int c = Integer.parseInt(l[i]);
temp.add(c);
}
ans_list.add(temp);
}
return ans_list;
}
先排序回溯法
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(candidates); //排序
getAnswer(ans, new ArrayList<>(), candidates, target, 0);
return ans;
} private void getAnswer(List<List<Integer>> ans, ArrayList<Integer> temp, int[] candidates, int target, int start) {
if (target == 0) {
ans.add(new ArrayList<Integer>(temp));
} else if (target < 0) {
return;
} else {
for (int i = start; i < candidates.length; i++) {
//跳过重复的数字
if(i > start && candidates[i] == candidates[i-1]) continue;
temp.add(candidates[i]);
/*************修改的地方*******************/
//i -> i + 1 ,因为每个数字只能用一次,所以下次遍历的时候不从自己开始
getAnswer(ans, temp, candidates, target - candidates[i], i + 1);
/****************************************/
temp.remove(temp.size() - 1);
}
}
}
[LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [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 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
- [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...
- leetcode 40 Combination Sum II --- java
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 ...
- Leetcode 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- Vue 2.x指令综合小练习
实现效果如下: 代码实现如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- 计算1+2+...+n
牛客上面一道题,闲来无事做做陶冶情操. 这一陶冶还真的陶冶出了骚操作 看一下题目吧: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及 ...
- vim实现批量注释和批量删除注释
批量注释 1.进入文档,vim test.txt 后,按住ctrl+v进入VISUAL BLOCK模式,上下选择需要注释的行 2.按大写键,再按i,或者直接按shift+i,进入INSERT模式,输入 ...
- JavaScript里的类和继承(转)
转自: http://www.h5cn.com/js/jishu/2016/0121/17634.html js与大部分客户端语言有几点明显的不同: JS是 动态解释性语言,没有编译过程,它在程序运行 ...
- Upgrading CentOS 6 to CentOS 7
Upgrading CentOS 6 to CentOS 7 November 15th, 2018 — whplus PRE TASKS There are some tasks you can d ...
- python学习-2 python安装和环境变量的设置
python的下载 1.可以去python官网下载,https://www.python.org/ 2.下载完成后,安装即可.(具体可以百度,网上都有很多安装方法) python的检测 1.打开开始- ...
- python os系统
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 hdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件 ...
- Python二、十、八进制相互转换
进制转换:先介绍用传统数学方法,再介绍用python内置方法 二进制转十进制: 1101 转为十进制 1*2^(4-1)+1*2^(3-1)+0*2^(2-1)+1*2^(1-1) 即各个位拆开,乘以 ...
- JSON运用在文件
#include <iostream>#include <fstream>#define JSON_IS_AMALGAMATION#include "json/jso ...
- hdu 1502 大数dp
对于每一个dp的问题 从其最优解的结构(分哪几种形式或者情况)入手 然后分析状态 这样就比较好找出状态转方程这里数据结构的选择很简单 顺序数组就可以 填充的方式顺序填充就可以 然后这道题目卡了我大数. ...