40. 组合总和 II

LeetCode_40

题目描述

题解分析

  1. 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下。
  2. 题解的关键是首先将候选数组进行排序,然后记录每个数的出现次数。
  3. 将去重后的数组当成是新的候选数组进行递归搜索。
  4. 回溯的时候注意是在最后将相同数字次数的数从列表中清除。

java代码

package com.walegarrett.interview;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* @Author WaleGarrett
* @Date 2021/2/27 17:53
*/ /**
* 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
* candidates 中的每个数字在每个组合中只能使用一次。
*/ /**
* 解法:回溯法
*/
public class LeetCode_40 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> list = new ArrayList<>();
List<int[]> map = new ArrayList<>();
Arrays.sort(candidates);
for(int num : candidates){
if(map.isEmpty() || num != map.get(map.size()-1)[0])
map.add(new int[]{num, 1});
else{
++map.get(map.size()-1)[1];
}
}
dfs(target, 0, list, result, map);
return result;
}
public void dfs(int target, int index, List<Integer> path, List<List<Integer>> result, List<int[]> map){
//找到一条路径
if(target == 0){
//注意:这里不能直接result.add(path),因为path是在回溯中会改变的,这样只存储了list的地址,地址是不变的。
result.add(new ArrayList<>(path));
return;
}
if(index == map.size() || target < map.get(index)[0])
return;
//跳过当前数
dfs(target, index+1, path, result, map);
//不跳过当前数
int ans = Math.min(map.get(index)[1], target/map.get(index)[0]);
for(int i=1; i<=ans; i++){
path.add(map.get(index)[0]);
dfs(target- i*map.get(index)[0], index+1, path, result, map);
}
for(int i=1; i<=ans; i++){
path.remove(path.size()-1);
}
}
}

40. 组合总和 II + 递归 + 回溯 + 记录路径的更多相关文章

  1. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  2. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  3. 40组合总和II

    题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...

  4. 40. 组合总和 II leetcode JAVA

    题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使 ...

  5. LeetCode 40. 组合总和 II(Combination Sum II)

    题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...

  6. leetcode 40. 组合总和 II (python)

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  7. 40. 组合总和 II

    题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只 ...

  8. Leetcode题库——40.组合总和II

    @author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...

  9. 组合总和 II

    组合总和 II 题目介绍 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...

随机推荐

  1. P3376 [模板] 网络最大流

    https://www.luogu.org/blog/ONE-PIECE/wang-lao-liu-jiang-xie-zhi-dinic EK 292ms #include <bits/std ...

  2. HDU-3499Flight (分层图dijkstra)

    一开始想的并查集(我一定是脑子坏掉了),晚上听学姐讲题才知道就是dijkstra两层: 题意:有一次机会能使一条边的权值变为原来的一半,询问从s到e的最短路. 将dis数组开成二维,第一维表示从源点到 ...

  3. Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad) C. Division (数学)

    题意:有两个数\(p\)和\(q\),找到一个最大的数\(x\),使得\(p\ mod\ x=0\)并且\(x\ mod\ q\ne 0\). 题解:首先,如果\(p\ mod\ q\ne0\),那么 ...

  4. Java基础(第二期)

    数据类型扩展以及面试题讲解 整数拓展:进制 int i=10; int i2=010; //八进制0 int i3=0x10; //十六进制0x 0~9 A~F 16 相关进制转换自行学习,用的不多 ...

  5. 宝塔面板&FLASK&centos 7.2 &腾讯云 配置网站出现的若干问题

    1.解决跨域问题&&中文显示问题 from flask import Flask, url_for, request, render_template, redirect from f ...

  6. codeforces 7B

    B. Memory Manager time limit per test 1 second memory limit per test 64 megabytes input standard inp ...

  7. C# 类 (12) - Partial

    Partial 前面说了,同一个namespace 里 class 名字是不能重的,除非是在不同的namespace里,下面开始打脸在同一个namespace里,加上partial 关键字,可以写同样 ...

  8. 力扣1689. 十-二进制数的最少数目-C语言实现-中等难度题

    题目 传送门 如果一个十进制数字不含任何前导零,且每一位上的数字不是 0 就是 1 ,那么该数字就是一个 十-二进制数 .例如,101 和 1100 都是 十-二进制数,而 112 和 3001 不是 ...

  9. cheerio & jQuery for server

    cheerio & jQuery for server Fast, flexible & lean implementation of core jQuery designed spe ...

  10. npm publish bug & solution

    npm publish bug & solution npm ERR! Unexpected token < in JSON at position 0 while parsing ne ...