[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 ... 
随机推荐
- Qt编写自定义控件25-自定义QCustomPlot
			一.前言 上次在写大屏数据可视化电子看板系统时候,提到过改造QCustomPlot来实现柱状分组图.横向柱状图.横向分组图.鼠标悬停提示等.这次单独列出来描述,有很多人疑问为啥不用QChart,或者e ... 
- Qt编写数据可视化大屏界面电子看板4-布局另存
			一.前言 布局另存是数据可视化大屏界面电子看板系统中的额外功能之一,主要用于有时候用户需要在现有布局上做个微调,然后直接将该布局另存为一个布局配置文件使用,可以省略重新新建布局重新来一次大的调整的工作 ... 
- leetcode刷题-559. Maximum Depth of N-ary Tree
			题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ... 
- 图解 HTTP 笔记(二)——简单的 HTTP 协议
			本章主要以 HTTP 1.0 为例,讲解 HTTP 协议的基本结构. 在两台计算机之间使用 HTTP 协议进行通讯时,在一条通讯线路上必定有一端是客户端,另一端则是服务器端. 请求访问文本或图像等资源 ... 
- HDFS数据定期清理
			HDFS数据清理一些办法: datanode数据做reblance清理临时目录.日志目录文件全量分区表历史分区清理使用lzo,orc格式进行数据压缩清理或者归档历史冷数据增加datanode横向扩容附 ... 
- LeetCode_9. Palindrome Number
			9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ... 
- 极客时间-左耳听风-程序员攻略-异步I/O模型和Lock-Free编程
			异步 I/O 模型 异步 I/O 模型其中的设计模式或是解决方法可以借鉴到分布式架构上来. 史蒂文斯(Stevens)在<UNIX 网络编程>一书 6.2 I/O Models 中介绍了五 ... 
- Flutter 页面下拉刷新和上拉加载
			flutter_easyrefresh 正如名字一样,EasyRefresh很容易就能在Flutter应用上实现下拉刷新以及上拉加载操作,它支持几乎所有的Flutter控件.它的功能与Android的 ... 
- MySQL创建用户、授权、删除
			1.在MySQL中创建新用户 使用具有shell访问权限的root用户登录MySQL服务器并创建名为“rahul”的新用户.下面的命令只允许从localhost系统访问用户rahul的MySQL服务器 ... 
- 详解Arduino Uno开发板的引脚分配图及定义(重要且基础)
			首先开发板实物图如下: 在本篇文章中,我们将详细介绍Arduino开发板的硬件电路部分,具体来说,就是介绍Arduino Uno开发板的引脚分配图及定义.Arduino Uno微控制器采用的是Atme ... 
