40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II
LeetCode_40
题目描述

题解分析
- 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下。
- 题解的关键是首先将候选数组进行排序,然后记录每个数的出现次数。
- 将去重后的数组当成是新的候选数组进行递归搜索。
- 回溯的时候注意是在最后将相同数字次数的数从列表中清除。
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 + 递归 + 回溯 + 记录路径的更多相关文章
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- Java实现 LeetCode 40 组合总和 II(二)
40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...
- 40组合总和II
题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...
- 40. 组合总和 II leetcode JAVA
题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使 ...
- LeetCode 40. 组合总和 II(Combination Sum II)
题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...
- leetcode 40. 组合总和 II (python)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- 40. 组合总和 II
题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只 ...
- Leetcode题库——40.组合总和II
@author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...
- 组合总和 II
组合总和 II 题目介绍 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...
随机推荐
- 【洛谷 p3371】模板-单源最短路径(图论)
题目:给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 解法:spfa算法. 1 #include<cstdio> 2 #include<cstdlib> 3 #in ...
- 仿ATM程序软件
一.目标: ATM仿真软件 1 系统的基本功能 ATM的管理系统其基本功能如下:密码验证机制:吞锁卡机制:存取款功能:账户查询功能:转账功能等. 要求 要能提供以下几个基本功能: (1)系统内的相关信 ...
- Hexo-使用阿里iconfont图标
Hexo-使用阿里iconfont图标 因为使用hexo搭建的博客中,大家并不懂都有什么图标,fa fa-xx就懵了,不知道都有什么. 首先,fa fa-xxx中的图标可以在 图标库 中寻找. (上面 ...
- SSH Keys vs GPG Keys
SSH Keys vs GPG Keys SSH Keys SSH keys allow you to establish a secure connection between your compu ...
- TypeScript & WebAssembly
TypeScript & WebAssembly WASM (module (func (param $lhs i32) (param $rhs i32) (result i32) local ...
- Versatile Python 3.x
Versatile Python 3.x TryPython Python 3.8.0 (default, Nov 14 2019, 22:29:45) [GCC 5.4.0 20160609] on ...
- Chrome offline game & source codes hacker
Chrome offline game & source codes hacker dino === little dinosaur chrome://dino/ 手动 offline htt ...
- live chat for website UX
live chat for website UX increase customer satisfaction using a live chat https://crisp.chat/en/live ...
- Windows 10 滚动截图工具
Windows 10 滚动截图工具 Edge & Note & Clip https://www.runoob.com/docker/docker-architecture.html ...
- chrome devtools dark mode
chrome devtools dark mode default dark mode https://medium.com/@eshwaren/dark-theme-for-chrome-devel ...