Problem

电池有6节包装,9节包装,20节包装三种,input需要多少节电池,如果可以刚好用3种包装的凑到这个数,就输出这个解, 忘了是不是要输出所有的解。
e.g 输入20, 答{20} 输入17 答没有 输入18,那可能是{6,6,6}也可能是{9,9}。 有点像找钱的问题,似乎是从集合中找到所有集合值等于一个target这个题的简化版,因为集合只有6 9 20。

Solution

 1 public static List<List<Integer>> combination(int[] num, int target) {
 2     List<List<Integer>> res = new ArrayList<List<Integer>>();
 3
 4     if(num == null || num.length == 0) return res;
 5
 6     List<Integer> path = new ArrayList<Integer>();
 7     helper(num, target, res, path);
 8     return res;
 9
10 }
11
12 public static void helper(int[] num, int target, List<List<Integer>> res, List<Integer> path) {
13
14     if(target == 0) {
15         res.add(new ArrayList<Integer>(path));
16         return;
17     }
18
19     if(target < 0)
20         return;
21
22     for(int i=0; i<num.length; i++) {
23         path.add(num[i]);
24         helper(num, target - num[i], res, path);
25         path.remove(path.size() - 1);
26     }
27 }

转载于:https://www.cnblogs.com/superbo/p/4112172.html

Battery (Coin Change)相关推荐

  1. Lightoj 1231 - Coin Change (I) (裸裸的多重背包)

    题目链接: Lightoj  1231 - Coin Change (I) 题目描述: 就是有n种硬币,每种硬币有两个属性(价值,数目).问用给定的硬币组成K面值,有多少种方案? 解题思路: 赤果果的 ...

  2. C++coin change 硬币找零(附完整源码)

    coin change 硬币找零 coin change 硬币找零算法的完整源码(定义,实现,main函数测试) coin change 硬币找零算法的完整源码(定义,实现,main函数测试) #in ...

  3. 利用动态规划(DP)解决 Coin Change 问题

    问题来源 这是Hackerrank上的一个比较有意思的问题,详见下面的链接: https://www.hackerrank.com/challenges/ctci-coin-change 问题简述 给 ...

  4. hdu2069(Coin Change)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069 Coin Change Time Limit: 1000/1000 MS (Java/Other ...

  5. LeetCode 518 Coin Change 2 (python)

    目录 LeetCode 518 Coin Change 2 (python) Code 欢迎一起来参与leetcode刷题项目 LeetCode 518 Coin Change 2 (python) ...

  6. Coin Change【硬币找零】

    一.题目 英文:Coin Change 中文:硬币找零 二.内容要求 英文:You are given coins of different denominations and a total amo ...

  7. Coin Change

    题目 Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to mak ...

  8. LeetCode Coin Change(动态规划)

    问题:给定不同面额的硬币coins和一个总金额amount,求凑成总金额所需的最少硬币个数 思路:第一种方法使用递归法,枚举x使用第coinIndex个硬币的amount/coins[coinInde ...

  9. leetcode 322. Coin Change | 322. 零钱兑换(动态规划)

    题目 https://leetcode.com/problems/coin-change/ 题解 也许是第一次在没看答案的情况下写的动态规划- 第一反应是,这题不是广义背包吗?想了一下,不是,因为广义 ...

  10. LeetCode 322. Coin Change

    原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...

最新文章

  1. mysql登录抓包_MySQL登录验证的抓包
  2. DL之U-Net:U-Net算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
  3. 小型的网站服务器配置,中小型网站服务器配置参考
  4. oracle recover redo,oracle redo log日志(当前或非当前日志)损坏之后的db恢复
  5. python 时分秒毫秒_python将时分秒转换成秒的实例
  6. axure html 360安装扩展,小编搞定win7系统360浏览器添加Axure扩展的设置方案
  7. 70个python毕设项目_56个具有开创性的Python开源项目-开始使用Python
  8. Tao 1.2.0图形框架发布
  9. xml提交数据的方法--gin
  10. UEditor 编辑器的下载与安装
  11. 什么是微信62数据?微信62数据原理解读
  12. 新版 | 小O地图V0.9.2.0
  13. vsCode 创建Vue项目
  14. Java web框架
  15. NXP的S32K系列MCU怎么样?
  16. ::before 和:after的区别
  17. 使用springboot每日推送早安问候语到用户微信【部署篇】
  18. 后台管理界面-- 管理员管理[4]
  19. 计算机附件计算器的用法,(计算机附件中的计算器使用方法.doc
  20. eshop1-大型电商架构演进

热门文章

  1. python html抓取,并用re正则表达式解析(一)
  2. docker-Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon runn
  3. iptables匹配statistic
  4. 深度学习是当前很热门的机器学习算法。在深度学习中,涉及到大量矩阵相乘,
  5. 土豆linux,土豆里安装linux是什_土豆发芽图片
  6. matlab画s域零极点图,信号与系统的S域分析.ppt
  7. windows linux 共存,Windows与Linux共存
  8. 固实压缩文件容易损坏_请慎重使用固实压缩文件
  9. 跨境电商国外消费者线上购物心理分析
  10. 我和《独角兽项目》背后的故事