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 ☆☆☆(数组相加等于指定的数)的更多相关文章

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

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

  2. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  3. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  4. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

  6. [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...

  7. leetcode 40 Combination Sum II --- java

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

  8. LeetCode 40. Combination Sum II (组合的和之二)

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

  9. Leetcode 40. Combination Sum II

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

随机推荐

  1. Tomcat 80端口被占用

    1.“运行”中输入cmd2.在命令行中输入netstat -ano,得到端口号对应的PID 3.打开任务管理器,点击“查看“菜单,选择“选择列”,给进程列表中添加”PID“列,然后找到PID对应的进程 ...

  2. 深度学习中的Attention机制

    1.深度学习的seq2seq模型 从rnn结构说起 根据输出和输入序列不同数量rnn可以有多种不同的结构,不同结构自然就有不同的引用场合.如下图, one to one 结构,仅仅只是简单的给一个输入 ...

  3. 最新 小米java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.小米等10家互联网公司的校招Offer,因为某些自身原因最终选择了小米.6.7月主要是做系统复习.项目复盘.LeetCode ...

  4. upw,c#,代码实现关闭,重启应用

    微软为uwp直接提供了这些API,可以很容易的直接进行调用 关闭 using Windows.ApplicationModel.Core; CoreApplication.Exit(); or usi ...

  5. javascript学习笔记 BOM和DOM详解

    js组成 我们都知道, javascript 有三部分构成,ECMAScript,DOM和BOM,根据宿主(浏览器)的不同,具体的表现形式也不尽相同,ie和其他的浏览器风格迥异. 1. DOM 是 W ...

  6. select示例

    #include <sys/select.h> #include <sys/time.h> #include <sys/types.h> #include < ...

  7. mysql中的反引号``

    [1]反引号`,数字1左边的符号.tab键上面的符号. 它是为了区分MYSQL的保留字与普通字符而引入的符号. 不加反引号建的表不能包含MYSQL保留字,否则出错 如上图,很明显的,如果我们直接建立名 ...

  8. linux利用crontab添加定时任务详解

    crontab 作用:添加,查询,删除系统计划任务的指令. [root@localhost ~]# crontab [选项]选项:    -e:    编辑crontab定时任务    -l:    ...

  9. web前端页面解决中文传参乱码问题

    问题背景:在项目中往往会涉及到前端跳转页面时要传一些参数给下一个页面,如果参数是英文或者数字的时候就很好解决,然而有时候传参会涉及到中文汉字,这个时候再单纯的拼接往往就会导致中文乱码,下面我们就该讨论 ...

  10. Eureka【支持Remote Region】

    工程公共pom依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncod ...