Given a number of different denominations of coins (e.g., 1 cent, 5 cents, 10 cents, 25 cents), get all the possible ways to pay a target number of cents.

Arguments

  • coins - an array of positive integers representing the different denominations of coins, there are no duplicate numbers and the numbers are sorted by descending order, eg. {25, 10, 5, 2, 1}
  • target - a non-negative integer representing the target number of cents, eg. 99

Assumptions

  • coins is not null and is not empty, all the numbers in coins are positive
  • target >= 0
  • You have infinite number of coins for each of the denominations, you can pick any number of the coins.

Return

  • a list of ways of combinations of coins to sum up to be target.
  • each way of combinations is represented by list of integer, the number at each index means the number of coins used for the denomination at corresponding index.

Examples

coins = {2, 1}, target = 4, the return should be

[

[0, 4],   (4 cents can be conducted by 0 * 2 cents + 4 * 1 cents)

[1, 2],   (4 cents can be conducted by 1 * 2 cents + 2 * 1 cents)

[2, 0]    (4 cents can be conducted by 2 * 2 cents + 0 * 1 cents)

]

public class Solution {
public List<List<Integer>> combinations(int target, int[] coins) {
// Write your solution here
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
helper(res, list, 0, coins, target);
return res;
} private void helper(List<List<Integer>> res, List<Integer> list, int index, int[] coins, int left) {
if (index == coins.length - 1) {
if (left % coins[coins.length - 1] == 0) {
list.add(left / coins[coins.length - 1]);
res.add(new ArrayList<>(list));
list.remove(list.size() - 1);
}
return;
}
for (int i = 0; i <= left / coins[index]; i++) {
list.add(i);
helper(res, list, index + 1, coins, left - i * coins[index]);
list.remove(list.size() - 1);
}
}
}

[Algo] 73. Combinations Of Coins的更多相关文章

  1. hdu 1398 Square Coins (母函数)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. hdu 1398 Square Coins(简单dp)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Pro ...

  3. Square Coins[HDU1398]

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. HDOJ 1398 Square Coins 母函数

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. Square Coins(母函数)

    Square Coins 点我 Problem Description People in Silverland use square coins. Not only they have square ...

  6. HDU1398 Square Coins(生成函数)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  7. ZOJ 1666 G-Square Coins

    https://vjudge.net/contest/67836#problem/G People in Silverland use square coins. Not only they have ...

  8. HDU 1398 Square Coins 整数拆分变形 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  9. HDU1398 Square Coins

    Description People in Silverland use square coins. Not only they have square shapes but also their v ...

随机推荐

  1. id就是方法名,如何调用;批量input怎么获取他们的key值作为参数

    1.很多Dom的时候,一个个写会比较麻烦,我用ID记载他的方法名: 2.很多input,在数据交互的时候一个个获取会比较繁琐,给一个方法,批量获取. <div id="searchSt ...

  2. Java 容器使用中如何选择

    Collection  ├List │├LinkedList │├ArrayList │└Vector │└Stack ├Queue │├Deque │└LinkedList └Set   ├Sort ...

  3. chr()//ord() //进制转换函数//eval()//文件函数//split()

    1.chr() 函数 chr() 用一个范围在 range(256)内的(就是0-255)整数作参数,返回一个对应的字符. 用法:chr(i) i可以是10进制也可以是16进制的形式的数字. 2.or ...

  4. 吴裕雄--天生自然MySQL学习笔记:MySQL 插入数据

    MySQL 表中使用 INSERT INTO SQL语句来插入数据. 可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 以下为向MySQL数据表插入数据通 ...

  5. go多态

      package main import ( "fmt" ) type Intf interface { process() } type MsgBase struct { id ...

  6. Mybatis实现联合查询(六)

    1. 疑问 在之前的章节中我们阐述了如何用Mybatis实现检查的查询,而我们实际的需求中,绝大部分查询都不只是针对单张数据表的简单查询,所以我们接下来要看一下Mybatis如何实现联合查询. 2. ...

  7. Eclipse 配置spring boot pom.xml第1行报错的两种解决办法

    现象 通过spring boot项目导入eclipse后,pom.xml文件的第一行总是报错.这里使用的spring版本是2.1.5,2.1.4以前的版本等其他版本的spring没有这个问题. 解决办 ...

  8. python版本,执行

    01. 第一个 HelloPython 程序 1.1 Python 源程序的基本概念 Python 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Python 的开发 Python 程 ...

  9. Java开学测试感想

    开学第一堂课就是测试,测试暑假的自学成果,老师说试卷适当提高了难度,所以允许查书和使用网络查询,经过近三个钟头的努力奋斗和痛苦挣扎,我只完成了一小部分的代码,只有简单的set()get()函数,以及简 ...

  10. linux虚拟机安装oracle全过程(一)

    起源:为方便系统迁移,导致的不必要的重装软件.故在虚拟机中搭建oracle数据库以及weblogic服务器(所有使用软件及资源链接附在结尾处) 1.安装虚拟机: 1.1.安装vmware 11破解版. ...