leetCode 39.Combination Sum(组合总和) 解题思路和方法

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 (a1, a2, … , 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]

思路:题意比较容易理解,首先想到的还是递归方法,思想是首先把=target的数保存到list,将<target的重新排列到数组,然后循环新的数组,寻找newArray,target-newarray[i]的最优解,最后将newlist添加上newArray[i]的值即可。

说的肯定不清楚,看代码,注释很详细:

public class Solution {
    public List<List<Integer>> combinationSum(int[] a, int t) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        Arrays.sort(a);//数组排序
        //各种特殊情况
        if(a.length == 0 || a[0] > t)
            return list;

        int len = 0;
        boolean isAdd = false;//控制与t相等的数只添加一次
        for(int i = 0; i< a.length;i++){
            if(a[i] == t){
                if(!isAdd){//如果未添加
                    List<Integer> al = new ArrayList<Integer>();
                    al.add(t);
                    list.add(al);
                    isAdd = true;//标记已添加
                }
            }else if(a[i] < t){//只要比target小的值,大的值肯定不满足,排除
                a[len++] = a[i];//新数组
            }
        }
        //只存在a[0] < target 或 a[0] > target
        if(a[0] > t)//肯定已没有符合要求的组合
            return list;
        //a[0] < target

        for(int i = 0; i < len; i++){//循环搜索符合要求的数字组合
        	int[] b = Arrays.copyOfRange(a, i, len);//不含>=t数据的新数组
        	if(a[i] > t)//如果a[i],肯定以后的数据已不符合,返回
        		break;
            //相等于已经有了一个值a[0]了
            List<List<Integer>> newList = new ArrayList<List<Integer>>();
            newList = combinationSum(b,t-a[i]);
            if(newList.size() > 0){//里面有符合要求的数据
                for(int j = 0; j < newList.size();j++){
                    newList.get(j).add(a[i]);//新返回的各个值添加a[0]
                    Collections.sort(newList.get(j));//排序
                    list.add(newList.get(j));//最终添加
                }
            }
        }
        return list;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 01:09:36

leetCode 39.Combination Sum(组合总和) 解题思路和方法的相关文章

[array] leetcode - 39. Combination Sum - Medium

leetcode - 39. Combination Sum - Medium descrition 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

Java [Leetcode 39]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)

【LeetCode】Combination Sum I &amp; II 解题报告

[Combination Sum I] 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 (inc

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 (in

LeetCode 39 Combination Sum(满足求和等于target的所有组合)

题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所有满足求和等于target的数字组合 遍历所有的数组中元素,然后对target进行更新,将该元素添加到tempList中,直到remain等于0时达到条件,可以将该tempList添加到list中 注意:每个元素可以使用多次,因此每次的遍历都要从上次的那个下标开始. 当target更新到小于0的时候,返

leetCode 47.Permutations II (排列组合II) 解题思路和方法

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

leetCode 64.Minimum Path Sum (最短路) 解题思路和方法

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:此题和前面几个机器人的题非常相像,只是变化了一点,具体代码和

19.2.3 [LeetCode 39] Combination Sum

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. The same repeated number may be chosen from candidates unlimited n

leetCode 67.Add Binary (二进制加法) 解题思路和方法

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路:二进制加法,比较简单.代码如下: public class Solution { public String addBinary(String a, String b) { int len = Math.max(a.len