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. Educational Codeforces Round 94 (Rated for Div. 2) String Similarity、RPG Protagonist、Binary String Reconstruction、Zigzags 思维

    题目链接:String Similarity 题意: 首先题目定义了两个串的相似(串的构成是0.1),如果两个串存在对于一个下标k,它们的值一样,那么这两个串就相似 然后题目给你一个长度为2n-1的串 ...

  2. hdu2126 Buy the souvenirs

    Problem Description When the winter holiday comes, a lot of people will have a trip. Generally, ther ...

  3. c++虚函数、子类中调用父类方法

    全部 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include< ...

  4. Selenium和ChromeDriver下载地址

    Selenium 官方所有版本: https://selenium-release.storage.googleapis.com/index.html 镜像所有版本:https://npm.taoba ...

  5. 4.PowerShell DSC核心概念之配置

    什么是配置 DSC 配置是定义某一特殊类型函数的 PowerShell 脚本. 配置的语法 Configuration MyDscConfiguration { #配置块 Import-DscReso ...

  6. Linux core dump使用

    什么是 core dump? core dump是一个当进程意外终止时包含进程内存内容的文件.当程序崩溃的时候,core dump由kernel触发.core dump可以作为程序崩溃时的事后快照(p ...

  7. Go中的Socket编程

    在很多底层网络应用开发者的眼里一切编程都是Socket,话虽然有点夸张,但却也几乎如此了,现在的网络编程几乎都是用Socket来编程.你想过这些情景么?我们每天打开浏览器浏览网页时,浏览器进程怎么和W ...

  8. CF1478-A. Nezzar and Colorful Balls

    CF1478-A. Nezzar and Colorful Balls 题意: 有\(n\)个球,每个球上面都有一个数字\(a_i\),这些数字是组成的序列是非递减的.现在你要给每个球涂色,你必须保证 ...

  9. Spring中AOP学习笔记

    AOP 描述(摘抄百度百科) AOP开发中的相关操作术语 实例(xml方式) 通知的类型 切入点表达式的写法: 实例(注解方式) 描述(摘抄百度百科) AOP(面向切面编程):Aspect Orien ...

  10. CodeForces 348D Turtles(LGV定理)题解

    题意:两只乌龟从1 1走到n m,只能走没有'#'的位置,问你两只乌龟走的时候不见面的路径走法有几种 思路:LGV定理模板.但是定理中只能从n个不同起点走向n个不同终点,那么需要转化.显然必有一只从1 ...