LeetCode OJ 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) 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]
Subscribe to see which companies asked this question
【题目解析】
给定一个候选数字序列一个目标值,找到所有的和等于目标值的候选数字组合。同一个候选数字可以出现多次。
1. 所有的数字是正数;
2. 组合中的数字降序排列;
3. 结果中不能存在相同的组合;
【思路】
首先我们从一个简单的例子分析一下这样组合生成的过程:
[1,2,3] target = 7
我们从[1,2,3]生成所有的和等于7的数字组合,可以分为以1开头的有:
[1,1,1,1,1,1,1],[1,1,1,1,1,2],[1,1,1,1,3],[1,1,1,2,2],[1,1,2,3],[1,2,2,2],[1,3,3]
以2开头的:
[2,2,3]
上面这几个组合是满足所有条件的组合。生成过程可以这样描述:
1. 给定一个升序排序的数组;
2. 从数组头部向后遍历,如果遇到一个比目标值小的数n,我们找到目标值为target - n的所有组合,如果找到这样的组合,那么我们把n合并到每一个组合里;
3. 如果遇到一个值m = target 则新建一个List,添加m后返回;
4. 如果遇到一个值m > target 则终结遍历,因为之后的数字肯定比target还大;
很明显这是一个递归的过程,在这个过程中我们首先对候选数组进行了排序,这是为什么呢? 因为在递归的过程中,如果一个数字n小于target,那么在递归求解target - n时我们可以确定一个从候选数组重新开始的下标,这个下标就是当前数字的下标。
【java代码】
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates); //升序排序
return combination(candidates, target, 0);
}
public List<List<Integer>> combination(int[] candidates, int target, int start) {
List<List<Integer>> list = new ArrayList<>();
if(candidates == null || candidates.length == 0) return list;
for(int i = start; i < candidates.length; i++){
if(candidates[i] < target){
List<List<Integer>> tlist = combination(candidates, target - candidates[i], i);
if(tlist.size() > 0){
for(List<Integer> alist : tlist){
alist.add(0, candidates[i]);
}
list.addAll(tlist);
}
}
else if(candidates[i] == target){
List<Integer> tlist = new LinkedList<>();
tlist.add(target);
list.add(tlist);
}
else break;
}
return list;
}
}
LeetCode OJ 39. Combination Sum的更多相关文章
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- LeetCode OJ 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode题解39.Combination Sum
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【一天一道LeetCode】#39. Combination Sum
一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode OJ:Combination Sum (组合之和)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode OJ:Combination Sum III(组合之和III)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- 记录下 js各种证件的正则验证
身份证 /(^\d{15}$)|(^\d{17}([0-9]|X)$)/ 护照 /^[a-zA-Z0-9]{3,21}$/ /^(P\d{7})|(G\d{8})$/ 军官证或士兵证 ...
- Chapter 2 Open Book——34
His gaze became appraising. "You put on a good show," he said slowly. 他的凝视变成了评价.“你上演了一场好戏” ...
- 将LibreOffice文档批量转成PDF格式
使用如下命令可以将文档一次性批量导出为pdf格式: -name -I /program/soffice.exe --headless --convert-to pdf '{}' find命令的-max ...
- 使用jquery.validate.js插件进行表单里控件的验证
jsp中具体实现的代码: <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- 从P1到P7——我在淘宝这7年(转)
作者: 赵超 发布时间: 2012-02-25 14:47 阅读: 114607 次 推荐: 153 [收藏] (一) 2011-12-08 [原文链接] 今天有同事恭喜我,我才知道自己在淘 ...
- jq小demo—图片翻页展示效果 animate()动画
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- iOS 导航栏 不透明
UINavigationBar.appearance().translucent = false UINavigationBar.appearance().barStyle = UIBarStyle. ...
- python操作----Memcached
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- Webstrom 常用操作记录
WebStorm 编译 es6 与 scss 的教程: http://blog.jetbrains.com/webstorm/2015/05/ecmascript-6-in-webstorm-tran ...
- python升级2.7.5
一开始有这个需求,是因为用 YaH3C 替代 iNode 进行校园网认证时,一直编译错误,提示找不到 Python 的某个模块,百度了一下,此模块是在 Python2.7 以上才有的,但是系统的自带的 ...