因为实验室项目好久没刷题了。从今天开始重新开始刷题。

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.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1a2 ≤ … ≤ ak).
  • 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是一个有序数组,按照非降序排列。等到第一次提交了才知道,给的就是一个无序的set。其实也无所谓。写了个快排,将数组排序。

将每一个元素一次加入到队列中,只与自己和比自己大的数进行叠加,这样可以防止重复。当相加和小于target时,将序列加入到队列中,大于的就不加入,相等的添加结果中。知道队列为空。


public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
       
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(candidates.length==0)
            return result;
        int start = 0;
        int end = candidates.length - 1;
        sort(candidates,start,end);
        if(candidates[0]>target)
            return result;
       
        while(start<=end&&candidates[start]<=target)
        {
            if(candidates[start] == target)
            {
                List<Integer> tem = new ArrayList<Integer>();
                tem.add(candidates[start]);
                result.add(tem);
                break;
            }
            result.addAll(sum(candidates,target,start,end));
            start++;                       
        }
       
       
        return result;
       
    }
   
    public List<List<Integer>> sum(int[] candidates,int target,int start,int end)
    {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        LinkedList<Sequence> queue = new LinkedList<Sequence>();
        Sequence sequ = new Sequence(candidates[start],start);
        queue.add(sequ);
        while(!queue.isEmpty())
        {
            Sequence element = queue.poll();
            for(int i=element.index;i<=end;i++)
            {
                if(element.sum + candidates[i]<target)
                {
                    Sequence temp = new Sequence();
                    List<Integer> list = new ArrayList();
                    list.addAll(element.seq);
                    list.add(candidates[i]);
                    temp.set(element.sum + candidates[i], list,i);
                    queue.addLast(temp);
                }
                else if(element.sum + candidates[i]==target)
                {
                    List<Integer> list = new ArrayList();
                    list.addAll(element.seq);
                    list.add(candidates[i]);
                    result.add(list);
                }
                else
                {
                    break;
                }
            }
        }
        return result;
    }
   
    public int partition(int[] sortArray,int low,int hight)
    {
           int key = sortArray[low];

while(low<hight)

{

while(low<hight && sortArray[hight]>=key)

hight--;

sortArray[low] = sortArray[hight];

while(low<hight && sortArray[low]<=key)
                    low++;
                sortArray[hight] = sortArray[low];
            }
            sortArray[low] = key;
            return low;
    }

public void sort(int[] sortArray,int low,int hight)

{

if(low<hight)

{

int result = partition(sortArray,low,hight);

sort(sortArray,low,result-1);

sort(sortArray,result+1,hight);

}

}
}

class Sequence
{
    List<Integer> seq =  new ArrayList();
    int sum;
    int index;
    public Sequence()
    {
        sum = 0;
    }
    public Sequence(int num,int index)
    {
        sum = num;
        seq.add(num);
        this.index = index;
    }
    public void set(int sum,List<Integer> list,int index)
    {
        seq = list;
        this.sum = sum;
        this.index = index;
    }
   
}

转载于:https://www.cnblogs.com/jessiading/p/3835333.html

LeetCode Combination Sum相关推荐

  1. LeetCode Combination Sum IV(动态规划)

    问题:给出一个数组nums和目标数target,问有多少组合形式 思路:用dp(i)表示目标数target的组合数.则有状态转移关系为dp(i)=sum(dp(i-nums[j])),其中i>= ...

  2. 回溯法和DFS leetcode Combination Sum

    代码: 个人浅薄的认为DFS就是回溯法中的一种,一般想到用DFS我们脑中一般都有一颗解法树,然后去按照深度优先搜索去寻找解.而分支界限法则不算是回溯,无论其是采用队列形式的还是优先队列形式的分支界限法 ...

  3. 【动态规划】LeetCode 377. Combination Sum IV

    LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...

  4. 【DFS】LeetCode 39. Combination Sum

    LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...

  5. 【LeetCode】#39组合总和(Combination Sum)

    [LeetCode]#39组合总和(Combination Sum) 加粗样式 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数 ...

  6. 40. Combination Sum II 组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  7. 216. Combination Sum III

    /** 216. Combination Sum III * 2016-6-12 by Mingyang* i一定要取到9,虽然大小聪明,想只取到7,但是后面的遍历可能也会遍历到9啊.* 1.长度标准 ...

  8. Combination Sum 和Combination Sum II

    这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在 ...

  9. 【同113】LeetCode 129. Sum Root to Leaf Numbers

    LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...

最新文章

  1. flask 配置文件
  2. Java-eclipse快捷键及设置
  3. 用tensorflow框架和Mnist手写字体,训练cnn模型以及测试一张手写字体
  4. 【easy】349. Intersection of Two Arrays
  5. CIO,你想做一辈子“消防员”吗?
  6. POJ1061 青蛙的约会(拓展欧几里德)
  7. 话单分析账单分析行踪分析综合数据研判软件
  8. 移动开发构架漫谈——反劫持实战篇
  9. 期末知识点复习——概率论与数理统计(5)
  10. 三本毕业,三年嵌入式软件的心路历程
  11. 游戏模型(3A)和影视模型(cg)的区别以及发展前景?
  12. Python的学习笔记案例8--空气质量指数计算1.0
  13. 一小时学会使用SpringBoot整合阿里云SMS短信服务
  14. GNN algorithms(3): Tri-party Deep Network Representation
  15. linux设备驱动--字符设备模型
  16. c 矩阵变matlab矩阵,怎么把一个函数变成矩阵形式
  17. 推荐 130 个令你眼前一亮的网站,总有一个用得着
  18. 25道Python工程师面试必备知识点!
  19. 个人征信报告解析(机构版)
  20. 计算机网络按其互连的距离远近,计算机网络按其互连的距离远近,可以分为:()。...

热门文章

  1. [Qt教程] 第27篇 XML(一)使用DOM读取XML文档
  2. mysql 脚本 linux_MySQL的一些功能实用的Linux shell脚本分享
  3. java pdf 书签_Java 展开或折叠PDF中的书签
  4. C中的危险函数(缓冲区溢出)
  5. 学习笔记-----关于C++中类的成员函数可以访问私有成员的问题
  6. Array Splitting CodeForces - 1197C
  7. 记录vmware的bug failed to install hcmon deriver
  8. 分割数组的最大值—leetcode410
  9. 探究java-JVM的五步(三步)类加载机制(包含类加载过程的一些代码书写,如类加载代码)
  10. 内存区域的划分和分配