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

    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 (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ 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] 

    One simple solution:

    public class Solution {
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            List<List<Integer> > result = new ArrayList<List<Integer> >();
    		Arrays.sort(candidates);
    		Deque<List<Integer> > combinations = new LinkedList<List<Integer> >();
    		List<Integer> startList = new ArrayList<Integer>();
    		combinations.add(startList);
    		Deque<Integer> sums = new LinkedList<Integer>();
    		Deque<Integer> lastIndices = new LinkedList<Integer>();
    		sums.add(0);
    		lastIndices.add(0);
    		int length = candidates.length;
    		while(combinations.peek() != null){
    			List<Integer> aList = combinations.poll();
    			int sum = sums.poll();
    			int lastIndex = lastIndices.poll();
    			for(int i = lastIndex; i < length; ++i){
    				if(sum + candidates[i] < target){
    					List<Integer> newList = new ArrayList<Integer>();
    					newList.addAll(aList);
    					newList.add(candidates[i]);
    					combinations.add(newList);
    					sums.add(sum + candidates[i]);
    					lastIndices.add(i);
    				}
    				else if(sum + candidates[i] == target){
    					aList.add(candidates[i]);
    					result.add(aList);
    				}
    				else
    					break;
    			}
    		}
    		return result;    
        }
    }
    

      

     Another solution:

     1 public class Solution {
     2     public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
     3        ArrayList<ArrayList<Integer> > result = new ArrayList<ArrayList<Integer> >();
     4         ArrayList<ArrayList<Integer> > temp = new ArrayList<ArrayList<Integer> >();
     5         ArrayList<IndexSum> sum = new ArrayList<IndexSum>();
     6         Arrays.sort(candidates);
     7         if(candidates.length > 0){
     8             for(int i = 0; i < candidates.length; ++i){
     9                 if(candidates[i] <= target){
    10                     ArrayList<Integer> nodes = new ArrayList<Integer>();
    11                     nodes.add(candidates[i]);
    12                     temp.add(nodes);
    13                     sum.add(new IndexSum(i, candidates[i]));                
    14                 }
    15                 else
    16                     break;;                
    17             }
    18             while(!temp.isEmpty()){
    19                 ArrayList<Integer> aNode = temp.remove(0);
    20                 IndexSum aPair = sum.remove(0);
    21                 if(aPair.sum == target)
    22                     result.add(aNode);
    23                 if(aPair.sum < target){
    24                     for(int i = aPair.index; i < candidates.length; ++i){
    25                         if(aPair.sum + candidates[i] < target){
    26                             ArrayList<Integer> newNode = new ArrayList<Integer>();
    27                             newNode.addAll(aNode);
    28                             newNode.add(candidates[i]);
    29                             temp.add(newNode);
    30                             IndexSum newPair = new IndexSum(i, aPair.sum + candidates[i]);
    31                             sum.add(newPair);
    32                         }
    33                         else if(aPair.sum + candidates[i] == target){
    34                             aNode.add(candidates[i]);
    35                             result.add(aNode);
    36                         }
    37                         else
    38                             break;
    39                     }
    40                 }
    41             }        
    42         }
    43         return result;        
    44     }
    45 }
    46 
    47 class IndexSum{
    48     int index;
    49     int sum;
    50     IndexSum(int i, int s){ index = i; sum = s;}
    51 }
  • 相关阅读:
    剑指Offer——对成的二叉树
    剑指Offer——二叉树的下一个节点
    路径总和I、II、III
    性能调优工具
    关于在程序中内存检测的一些知识
    ptmalloc、tcmalloc及 jemalloc总结
    [LeetCode] 43. 字符串相乘
    [LeetCode] 155. Min Stack
    [LeetCode] 380. Insert Delete GetRandom O(1)
    linux内存过高排查
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3536563.html
Copyright © 2011-2022 走看看