zoukankan      html  css  js  c++  java
  • Combination Sum leetcode java

    题目:

    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]

    题解

    还是老问题,用DFS找解决方案,不同点是,这道题: The same repeated number may be chosen from C unlimited number of times.

    所以,每次跳进递归不用都往后挪一个,还可以利用当前的元素尝试。

    同时,这道题还要判断重复解。用我之前介绍的两个方法:

     1.       if(i>0 && candidates[i] == candidates[i-1])//deal with dupicate
                     continue

     2.       if(!res.contains(item))
                    res.add(new ArrayList<Integer>(item));  

    这两个方法解决。

    代码如下:

     1     public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {  
     2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();  
     3         ArrayList<Integer> item = new ArrayList<Integer>();
     4         if(candidates == null || candidates.length==0)  
     5             return res; 
     6             
     7         Arrays.sort(candidates);  
     8         helper(candidates,target, 0, item ,res);  
     9         return res;  
    10     }  
    11     
    12     private void helper(int[] candidates, int target, int start, ArrayList<Integer> item,   
    13     ArrayList<ArrayList<Integer>> res){  
    14         if(target<0)  
    15             return;  
    16         if(target==0){  
    17             res.add(new ArrayList<Integer>(item));  
    18             return;  
    19         }
    20         
    21         for(int i=start;i<candidates.length;i++){  
    22             if(i>0 && candidates[i] == candidates[i-1])//deal with dupicate
    23                 continue;  
    24             item.add(candidates[i]);
    25             int newtarget = target - candidates[i];
    26             helper(candidates,newtarget,i,item,res);//之所以不传i+1的原因是:
    27                                                     //The same repeated number may be 
    28                                                     //chosen from C unlimited number of times.
    29             item.remove(item.size()-1);  
    30         }  
    31     } 

  • 相关阅读:
    Quartz.NET总结(八)如何根据自己需要配置Topshelf 服务
    Golang 入门系列(十七)几个常见的并发模型——生产者消费者模型
    能避开很多坑的mysql面试题,你知道吗?
    Golang 入门系列(十六)锁的使用场景主要涉及到哪些?读写锁为什么会比普通锁快
    Thrift总结(四)Thrift实现双向通信
    Golang 入门系列(十五)如何理解go的并发?
    Nginx总结(六)nginx实现负载均衡
    Nginx总结(五)如何配置nginx和tomcat实现反向代理
    Nginx总结(四)基于域名的虚拟主机配置
    Nginx总结(三)基于端口的虚拟主机配置
  • 原文地址:https://www.cnblogs.com/springfor/p/3884294.html
Copyright © 2011-2022 走看看