Leetcode39--->Combination Sum(在数组中找出和为target的组合)

题目: 给定一个数组candidates和一个目标值target,求出数组中相加结果为target的数字组合;

举例:

For example, given candidate set [2, 3, 6, 7] and target 7
A solution set is: 

[[7],[2, 2, 3]]

从举例中可以看出,同一个数字可以使用多次,且结果是唯一的;

解题思路:

我个人感觉该题目一点也不难,其实也是一个递归的过程,当达到递归条件时,就将结果加入结果集;

首先题目没说给的数组有啥特性,因此我先将数组进行了排序,这样在某个点找不着结果,那后面的都比target大,自然也就没有结果了。废话不多说,直接看代码;

代码如下:

 1 import java.util.*;
 2 public class Solution {
 3     public List<List<Integer>> combinationSum(int[] candidates, int target) {
 4         List<List<Integer>> LList = new ArrayList<List<Integer>>();  // 最终的结果集
 5         if(candidates == null || candidates.length < 1 || target < 1 )
 6             return LList;
 7         Arrays.sort(candidates);  // 排序,使得不用对相同的结果集计算多次
 8         List<Integer> list = new ArrayList<Integer>();  // 临时结果保存
 9         combinationSumCore(candidates,list, target, 0, LList);  // 核心函数
10         return LList;
11     }
12     public void combinationSumCore(int[] candidates,List<Integer> list, int target, int index, List<List<Integer>> LList)
13     {
14         for(int i = index; i < candidates.length; i++) 
15         {
16             if(candidates[i] == target)  // 等于,就加入结果集
17             {
18                 List<Integer> result = new ArrayList<Integer>();
19                 result.addAll(list);
20                 result.add(candidates[i]);
21                 LList.add(result);
22             }
23             else if(candidates[i] < target)  // 小于,就继续递归
24             {
25                 List<Integer> result = new ArrayList<Integer>();
26                 result.addAll(list);
27                 result.add(candidates[i]);
28                 combinationSumCore(candidates, result, target - candidates[i], i, LList);  // 这边i值不变,是因为当前值可以使用多次
29             }
30             else  // 大于,则后面的数字都大于,因此不可能出现在结果集中
31             {
32                 break;
33             }
34         }
35     }
36 }

 

转载于:https://www.cnblogs.com/leavescy/p/5900943.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下代码来创建一个函数,用于给定一个整数数组nums和一个整数目标值target,并找出和为目标值target的那两个整数,并返回它们的数组下标: def twoSum(nums, target): num_dict = {} for i in range(len(nums)): complement = target - nums[i] if complement in num_dict: return [num_dict[complement], i] num_dict[nums[i]] = i return [] nums = [2, 7, 11, 15] target = 9 result = twoSum(nums, target) print(result) 这个函数使用了一个字典来存储已经遍历过的数字及其对应的下标。在每次遍历数组时,我们计算出目标值与当前数字的差值,然后检查这个差值是否存在于字典中。如果存在,则说明找到了目标值的两个整数,返回它们的下标;如果不存在,则将当前数字及其下标存入字典中。通过这种方式,我们可以在一次遍历中找到答案,而不需要使用两个for循环进行遍历。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [leetcode两数之和python](https://download.csdn.net/download/weixin_38558623/13743171)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [给定一个整数数组 nums和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那两个整数,并...](https://blog.csdn.net/qq_57732418/article/details/125411024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值