leetcode 39 java dfs
来源:互联网 发布:萧山问政网络平台 编辑:程序博客网 时间:2024/06/07 10:12
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7]
and target 7
,
A solution set is:
[ [7], [2, 2, 3]]
对于输入candidates=[1,2] ,target=3,遍历的方向如图:
AC代码
public class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<Integer> path =new ArrayList<Integer>();List<List<Integer>> result=new ArrayList<>();Arrays.sort(candidates);dfs(candidates,0,0,target,path,result);return result; } private void dfs(int[] a, int now, int sum, int target,List<Integer> path,List<List<Integer>> result) {// TODO Auto-generated method stubif(sum==target){result.add(new ArrayList<>(path));return ;}if(sum>target){return ;}for(int i=now;i<a.length;i++){if(sum+a[i]>target){break;}path.add(a[i]);System.out.println("dfs("+i+")");dfs(a,i,sum+a[i],target,path,result);path.remove(path.size()-1);}}}
阅读全文
0 0
- leetcode 39 java dfs
- leetcode DFS
- [Leetcode] DFS
- leetcode DFS
- LeetCode-39-Combination Sum(DFS)-Medium
- java dfs
- LeetCode Word Break(DFS)
- Leetcode dfs Subsets
- Leetcode dfs Path Sum
- Leetcode dfs Combinations
- Leetcode dfs Combination Sum
- Leetcode dfs Combination SumII
- Leetcode dfs Sudoku Solver
- Leetcode dfs Path SumII
- Leetcode dfs N-Queens
- Leetcode dfs&dp Triangle
- leetcode dfs Subsets II
- leetcode dfs Palindrome Partitioning
- n个珠子m种颜色,包含全部m种颜色的最短子串长度
- 通过PyTorch实现对抗自编码器
- 用myeclipse构建maven项目
- Linux内核源码分析--内存管理(二、函数实现技巧)
- 3^3+4^3+5^3=6^3
- leetcode 39 java dfs
- Windows 7,8,8.1,10 NTP Server
- android studio移除ndk路径的方法,略笨
- 梦网云通讯获取API上行接口get_mo说明POST实例
- UE4-XML与JSon的简单读写实现
- 《Android源码设计模式解析与实战》读书笔记(二十三)——外观模式
- echart 重叠 以及添加更多的图例
- 机器学习--监督学习之梯度下降法
- 关于阿里云服务器上使用javamail失败的解决方案