leetcode 39. Combination Sum

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]
]


主要的思路是这一次是用这个数字还是不用这个数字
import java.util.LinkedList;
import java.util.List;


public class Solution39 {
    public static List<List<Integer>> ans = new LinkedList<List<Integer>>();
    public static LinkedList<Integer> list = new LinkedList<Integer>();
    public static void robot(int idx,int c[],int target){
        if (target==0) {
            System.out.println(list);
            List<Integer> list2 = new LinkedList<Integer>();
            for (Integer i : list) {
                list2.add(i);
            }
            ans.add(list2);
            return;
        }
        if (target<0||idx>=c.length) {
            return;
        }
        list.add(c[idx]);
        robot(idx, c, target-c[idx]);
        list.pollLast();
        robot(idx+1, c, target);
    }
    
    public static void main(String[] args) {
        int arr[]={2, 3, 6, 7};
        robot(0, arr, 7);

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,357评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,508评论 0 23
  • Given a set of candidate numbers (C) and a target number ...
    Eazow阅读 1,153评论 0 0
  • 也许,最美的花开在灵魂深处从此,她的诗里没有妖艳与妩媚一个人把另一个人的魂魄带走了留于尘世的只能是一具空壳和众生面...
    风之子的黄昏阅读 3,034评论 15 15
  • 进入打卡的第二天,突然有点举步维艰,本以为会越来越熟悉,可突然提笔忘句,写了删,删了写,总也继续不下去。 写作这事...
    易水微尘阅读 1,540评论 4 0

友情链接更多精彩内容