zoukankan      html  css  js  c++  java
  • leecode-39. Combination Sum

    1、问题描述

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

    2、边界条件:无
    3、思路:先取一个数,然后与target比较;==则存贮,!=则继续从所有数里面选择。

      从Level-N里面选择一个数,与目标比较,符合则存贮;不符合则再从所有数里面挨个取,从而化为同样的Level-N问题
    形成递归。base case:1)与目标匹配;2)target - nums[i]<0,再减下去也是负数,这依赖于题目给的 全正数positive integers 条件
    4、实现代码:
    
    
    class Solution {
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            List<List<Integer>> results = new ArrayList<>();
            //Arrays.sort(candidates);
            combinationSum(results, new ArrayList<Integer>(), candidates, target);
            return results;
        }
        
        public void combinationSum(List<List<Integer>> results, List<Integer> cur, int[] candidates, int target) {
            if (0 == target) {
           /** Collections.sort(cur);//这里排序会把原列表改变,所以上层在恢复现场时出错。
    if (!results.contains(cur)) {//去重 results.add(new ArrayList<Integer>(cur)); }
           **/

            ArrayList<Integer> result = new ArrayList<Integer>(cur);//先生成新的cur,然后进行排序
            Collections.sort(result); //
            if (!results.contains(result)) {
              results.add(result);
                   return;

            }
            if (0 > target) {
                return;
            }
            for (int i = 0; i < candidates.length; i++) {
                cur.add(candidates[i]);
                combinationSum(results, cur, candidates, target - candidates[i]);
                cur.remove(cur.size() - 1);
            }
        }
    }

    5、时间复杂度:说不好; 空间复杂度:

    6、题外知识:Arraylist排序:Collections静态排序API,Collections的排序都是稳定的。Collections.sort(List<T> list)、和Collections.sort(List<T> list,Comparator<?super T> c);使用的排序是稳定的,主要是对list排序。

    链接:http://blog.csdn.net/tuke_tuke/article/details/51100219 和 http://www.importnew.com/17211.html  
    7、优化解法
    class Solution {
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            List<List<Integer>> results = new ArrayList<>();
            Arrays.sort(candidates);//排序是为了使得答案有序;如果有重复数字的情况下,可以方便去重。
            combinationSum(results, new ArrayList<Integer>(), candidates, target, 0);
            return results;
        }
    
        public void combinationSum(List<List<Integer>> results, List<Integer> cur, int[] candidates, int target, int start) {
            if (0 == target) {
                results.add(new ArrayList<Integer>(cur));
                return;
            }
            if (0 > target) {
                return;
            }
            for (int i = start; i < candidates.length; i++) { ///从start开始是因为前面的数字已经遍历过自己和后面的数字。
                cur.add(candidates[i]);
                combinationSum(results, cur, candidates, target - candidates[i], i);// not i + 1 because we can reuse same elements
                cur.remove(cur.size() - 1);
            }
        }
    }
     
    
    
    
     
  • 相关阅读:
    POJ 3259 Wormholes【BellmanFord】
    POJ 2960 SNim【SG函数的应用】
    ZOJ 3578 Matrixdp水题
    HDU 2897 邂逅明下【bash博弈】
    BellmanFord 算法及其优化【转】
    【转】几个Java的网络爬虫
    thinkphp 反字符 去标签 自动加点 去换行 截取字符串 冰糖
    php 二维数组转 json文本 (jquery datagrid 数据格式) 冰糖
    PHP 汉字转拼音(首拼音,所有拼音) 冰糖
    设为首页与加入收藏 兼容firefox 冰糖
  • 原文地址:https://www.cnblogs.com/shihuvini/p/7440170.html
Copyright © 2011-2022 走看看