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大,自然也就没有结果了。废话不多说,直接看代码;
代码如下:
import java.util.*;
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> LList = new ArrayList<List<Integer>>(); // 最终的结果集
if(candidates == null || candidates.length < 1 || target < 1 )
return LList;
Arrays.sort(candidates); // 排序,使得不用对相同的结果集计算多次
List<Integer> list = new ArrayList<Integer>(); // 临时结果保存
combinationSumCore(candidates,list, target, 0, LList); // 核心函数
return LList;
}
public void combinationSumCore(int[] candidates,List<Integer> list, int target, int index, List<List<Integer>> LList)
{
for(int i = index; i < candidates.length; i++)
{
if(candidates[i] == target) // 等于,就加入结果集
{
List<Integer> result = new ArrayList<Integer>();
result.addAll(list);
result.add(candidates[i]);
LList.add(result);
}
else if(candidates[i] < target) // 小于,就继续递归
{
List<Integer> result = new ArrayList<Integer>();
result.addAll(list);
result.add(candidates[i]);
combinationSumCore(candidates, result, target - candidates[i], i, LList); // 这边i值不变,是因为当前值可以使用多次
}
else // 大于,则后面的数字都大于,因此不可能出现在结果集中
{
break;
}
}
}
}
Leetcode39--->Combination Sum(在数组中找出和为target的组合)的更多相关文章
- Leetcode33--->Search in Rotated Sorted Array(在旋转数组中找出给定的target值的位置)
题目: 给定一个旋转数组,但是你不知道旋转位置,在旋转数组中找出给定target值出现的位置:你可以假设在数组中没有重复值出现 举例: (i.e., 0 1 2 4 5 6 7 might becom ...
- 用C#写一个函数,在一个数组中找出随意几个值相加等于一个值 与迭代器对比
算法!用C#写一个函数,在一个数组中找出随意几个值相加等于一个值比如,数组{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20} 要找出那些数相加等 ...
- 刷题之给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数
今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的 ...
- C语言 选择排序算法原理和实现 从数组中 找出最小的元素然后交换位置
#include <stdio.h> int main(void) { /* 选择排序算法 原理:从数组中 找出最小的元素然后交换位置: */ int a[10] = {9,5,10,7, ...
- 从数组中找出所有组合为s的数
java版本 package numCombine; /** * 从数组中找出所有组合为s的数 * @author root * */ public class NumComberAll { publ ...
- C语言:对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中。-在数组中找出最小值,并与第一个元素交换位置。
//对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中. #include <stdio.h& ...
- Leetcode34--->Search for a Range(在排序数组中找出给定值出现的范围)
题目:给定一个排序数组,找出给定的target值出现的范围:算法复杂度要求在O(logn);如果没有找到,则返回[-1, -1]; 举例: For example,Given [5, 7, 7, 8, ...
- 3sum(从数组中找出三个数的和为0)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 数组中找出最小的K个数
题目 给出一个数组,找出K个最小的值 例如给出数组{5,2,4,3,1},给定K值3,则输出结果为{2,3,1} 程序 先给出第一个版本的程序 public static void printKNum ...
随机推荐
- ES6 Interator
Interator "集合"数据的结构主要有 Array .Object. Set and Map ,任何数据结构只要部署 Iterator 接口,就可完成遍历操作 遍历过程: 创 ...
- Android笔记--LayoutInflator源码和使用分析
LayoutInflator源码分析 获取LayoutInflator对象 获取LayoutInflator的方式有两种: 使用LayoutInflator.from(Context context) ...
- getuser
Help on function getuser in module getpass: getuser() Get the username from the environment or pa ...
- python os,sys模块的使用
首先,os模块是用来与操作系统进行交互的模块,可以对操作系统上的一些东西进行操作 而sys是用来对解释器进行一些操作的 一.os os.getcwd() 获取当前工作目录,即当前python脚本工作的 ...
- Python之时间表示
Python的time模块中提供了丰富的关于时间操作方法,可以利用这些方法来完成这个需求. time.time() :获取当前时间戳 time.ctime(): 当前时间的字符串形式 time.loc ...
- 2017.10.6 QBXT 模拟赛
题目链接 T1 Sort 一下与原数组比较 ,若有两个数或者没有数发生位置交换 ,则输出YES ,否则输出NO #include <algorithm> #include <ccty ...
- 洛谷 P1001 A+B Problem
题目描述 输入两个整数a,b,输出它们的和(|a|,|b|<=10^9). 注意 1.pascal使用integer会爆掉哦! 2.有负数哦! 3.c/c++的main函数必须是int类型,而且 ...
- [VC]ocx控件怎么屏蔽backspace的后退键
<script Language=javascript> function document.onkeydown() { if(window.event.keyCode = ...
- Core Data stack
https://developer.apple.com/library/content/documentation/DataManagement/Devpedia-CoreData/coreDataS ...
- FMDB的使用方法(附Demo)
http://www.jianshu.com/p/54e74ce87404 最近在项目中需要在多个页面对同样的数据进行相关操作,于是便用到了FMDB数据库操作,以下便是FMDB的一些简单的使用方法.附 ...