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)

]

  1. public class Solution {
  2. public List<List<Integer>> combinations(int target, int[] coins) {
  3. // Write your solution here
  4. List<List<Integer>> res = new ArrayList<>();
  5. List<Integer> list = new ArrayList<>();
  6. helper(res, list, 0, coins, target);
  7. return res;
  8. }
  9.  
  10. private void helper(List<List<Integer>> res, List<Integer> list, int index, int[] coins, int left) {
  11. if (index == coins.length - 1) {
  12. if (left % coins[coins.length - 1] == 0) {
  13. list.add(left / coins[coins.length - 1]);
  14. res.add(new ArrayList<>(list));
  15. list.remove(list.size() - 1);
  16. }
  17. return;
  18. }
  19. for (int i = 0; i <= left / coins[index]; i++) {
  20. list.add(i);
  21. helper(res, list, index + 1, coins, left - i * coins[index]);
  22. list.remove(list.size() - 1);
  23. }
  24. }
  25. }

[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. (排序)P1177 【模板】快速排序

    题解: 这道题用传统快排(如下所示)的结果就是最后三个点TLE: 如果永远取第一个元素作为枢轴的话,在数组已经有序的情况下每次划分都将得到最坏的结果,时间复杂度退化为O(n^2).因为其中一个子序列每 ...

  2. python----linux下简单的排序

    1.选择排序:把一个数与余下所有的数排序,最小的排到最前面 [root@besttest liyn_test]# cat test.py #! /usr/bin/python a=[,,,] ,len ...

  3. git 一些操作

    1. 代码相关 克隆代码 git clone xxx.git 拉取代码 git pull 查看 修改的 状态 git status 推送代码 git push add 或者 修改代码之后 回滚到 未修 ...

  4. 对python里的装饰器

    内裤可以用来遮羞,但是到了冬天它没法为我们防风御寒,聪明的人们发明了长裤,有了长裤后宝宝再也不冷了,装饰器就像我们这里说的长裤,在不影响内裤作用的前提下,给我们的身子提供了保暖的功效. 再回到我们的主 ...

  5. linux centos 7 防火墙相关

    centos 7 系统 默认是开启防火墙,而且没有打开80和8080等端口. 因此,今天配置tomcat和nginx后,分别无法正常访问 访问80和8080端口都报:502错误.(错误的网关)查询资料 ...

  6. WordPress迁移服务器后报Nginx404的问题

    Wordpress迁移服务器后,只有主页能打开,其它页面都显示404 页面无法访问. 出现这个问题是因为我的Wordpress之前用的服务器是apache+PHP组合,换了服务器后变成了Nginx+P ...

  7. gitee上传下载代码命令

    在想要下载的文件夹下,鼠标右键,git bash 1.输入git init 进行初始化 2.git remote add origin https://gitee.com/XXXXXXXXXXXXXX ...

  8. http head详解

    Http普通报头:       少数报头域用于所有的请求和响应消息, 但并不用于被传输的实体 cache-Control: 用于指定缓存指令, 缓存指令是单向的 ,且是独立的(一个消息的缓存指令不会影 ...

  9. linux 解压命令总结

    常用Linux 命令: [转自]https://www.jianshu.com/p/ca41f32420d6 解压缩 .tar 解包:tar xvf FileName.tar 打包:tar cvf F ...

  10. java 练习题带答案

    第一题 int x = 1,y=1; if(x++==2 & ++y==2) { x =7; } System.out.println("x="+x+",y=&q ...