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

Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

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 10,1,2,7,6,1,5 and target 8,

A solution set is:

[1, 7]

[1, 2, 5]

[2, 6]

[1, 1, 6]

思路:此题和Combination Sum类同,而且很多代码都可以复用,仅仅改变几处不大的变化和增加了一个去处重复项的函数。

具体代码如下:

public class Solution {

    public List<List<Integer>> combinationSum2(int[] a, int t) {
		List<List<Integer>> list = new ArrayList<List<Integer>>();
		List<List<Integer>> newList = new ArrayList<List<Integer>>();
		list = combinationSum1(a,t);
        Set<List<Integer>> set = new HashSet<List<Integer>>();//去除重复
        for(int i = 0; i < list.size();i++){
        	if(set.add(list.get(i))){//没有重复
        		newList.add(list.get(i));//添加新的列表
        	}
        }
        return newList;
	}

    public List<List<Integer>> combinationSum1(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+1, len);//不含>=t数据的新数组
        	if(a[i] > t)//如果a[i],肯定以后的数据已不符合,返回
        		break;
            //相等于已经有了一个值a[0]了
            List<List<Integer>> newList = new ArrayList<List<Integer>>();
            if(i < len -1)
            	newList = combinationSum1(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-20 07:35:49

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

leetCode 112.Path Sum (路径和) 解题思路和方法

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 思路:求解二叉树的路径和是否满足

[array] leetcode - 40. Combination Sum II - Medium

leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat

Java [Leetcode 40]Combination Sum II

题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will b

LeetCode 40. Combination Sum II (组合的和之二)

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be posi

LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数.(数组中的元素可能有重复) 求出所有的满足求和等于terget的组合. 数组中的元素只能使用一次.(数组中重复的元素可以最多使用重复次数) 参考代码: package leetcode_50; import java.util.ArrayList; import java.util.Arrays; im

leetcode 40 Combination Sum II --- java ----100%

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be posi

[LeetCode] #40 Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be posi

[LeetCode] 40. Combination Sum II Java

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

19.2.3 [LeetCode 40] Combination Sum II

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All nu