关闭

leetcode-java-39. Combination Sum

标签: leetcodejava
97人阅读 评论(0) 收藏 举报
分类:
/*
Given a set of candidate numbers (C) 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]
]
 */
/*
思路l:
深度优先搜索即可。
这个用递归来实现,递归终止条件:
1.如果sum>target直接返回
2.如果sum=target则保存结果
但需要注意的:
每次往result存储时,需要new ArrayList<Integer>,以免替换了以前的值
 */
 public class Solution {
     public List<List<Integer>> result;
     public List<Integer> seq;

     public List<List<Integer>> combinationSum(int[] candidates, int target) {
         result  = new ArrayList<List<Integer>>();
         seq = new ArrayList<Integer>();

         Arrays.sort(candidates);
         findPaths(candidates,target,0,0);

         return result;
     }

     public void findPaths(int[] candidates,int target,int sum,int level) {
         if(target == sum) {
             result.add(new ArrayList<Integer> (seq));
             return;
         }
         if(target < sum) {
             return;
         }

         for(int i = level;i < candidates.length;i++) {
            //  相当于是当前节点压入栈
             seq.add(candidates[i]);
             sum += candidates[i];
             findPaths(candidates,target,sum,i);
            //  没返回值,也就是当前节点不符合,则弹出栈的意思
             seq.remove(seq.size() - 1);
             sum -= candidates[i];
         }
     }
 }
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

leetCode 39.Combination Sum(组合总和) 解题思路和方法

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique com...
  • xygy8860
  • xygy8860
  • 2015-07-09 22:33
  • 1140

【LeetCode-面试算法经典-Java实现】【113-Path Sum II(路径和)】

【113-Path Sum II(路径和II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree and a sum, find al...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-12 06:23
  • 2492

【LeetCode-面试算法经典-Java实现】【112-Path Sum(路径和)】

【112-Path Sum(路径和)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree and a sum, determine if...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-11 07:40
  • 2795

(Java)LeetCode-39. Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher...
  • u012848330
  • u012848330
  • 2016-08-10 22:35
  • 189

回溯详解及其应用:Leetcode 39 combination sum

原理 初入门 基本定义和概念 举栗子 编程思路 实践 生成符合规范的括号 combination sum n queen原理初入门有时会遇到这样一类题目,它的问题可以分解,但是又不能得出明确的动态规划...
  • c602273091
  • c602273091
  • 2017-01-31 16:47
  • 345

LeetCode 39:Combination Sum

每一次向下搜索时,起始位置都和上一次相同,因为可以取相同元素不止一次,即每次向下传入的index For循环每次从index开始,避免返回到之前的元素 DFS搜索 如果sum>target 则r...
  • dingchenxixi
  • dingchenxixi
  • 2016-12-01 20:29
  • 220

Leetcode 39. Combination Sum

Leetcode 39.COmbination Sum
  • Earl211
  • Earl211
  • 2017-06-22 21:46
  • 113

[LeetCode]39.Combination Sum

【题目】 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in...
  • SunnyYoona
  • SunnyYoona
  • 2015-01-27 17:47
  • 2637

LeetCode 39. Combination Sum 解题博客

LeetCode 39. Combination Sum 解题报告题目描述Given a set of candidate numbers (C) (without duplicates) and a...
  • camellhf
  • camellhf
  • 2017-06-14 21:02
  • 382

(Leetcode)39&40. Combination Sum--Using Backtracking

Problem39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all ...
  • muyu709287760
  • muyu709287760
  • 2016-07-01 13:46
  • 356
    个人资料
    • 访问:104344次
    • 积分:2945
    • 等级:
    • 排名:第13864名
    • 原创:198篇
    • 转载:4篇
    • 译文:0篇
    • 评论:9条
    文章分类
    最新评论