back function (return number)

remember the structure

class Solution {
int res = 0;
//List<List<Integer>> resList = new ArrayList<List<Integer>>();
public int combinationSum4(int[] nums, int target) {
Arrays.sort(nums);
return back(target, 0,nums,new HashMap<Integer,Integer>());
}
int back(int target, int sum, int[] nums, Map<Integer,Integer> map){
if(sum == target){
return 1;
}else if(sum > target) return 0;
if(map.containsKey(sum)) return map.get(sum);
int count = 0;
for(int i = 0; i<nums.length; i++){
count+= back(target, sum+nums[i],nums,map);
}
map.put(sum,count);
return count;
}
}

Solution 2:

dp keywards: how many ways and optimal

class Solution {
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target+1]; // how many cases for each number
Arrays.sort(nums);
for(int num:nums){
if(num>target) continue;
dp[num] = 1;
}
for(int i = 1;i <=target; i++){ for(int num : nums){
if(i<num) continue;
dp[i] += dp[i-num];
} }
return dp[target];
}
}

70. Climbing Stairs

class Solution {
//dp[n] = dp[n-1] + dp[n-2]
//dp[1] : 1, dp[0] = 1 ,dp[2] = 2, dp[3] = 3
public int climbStairs(int n) {
int[] dp = new int[n+1];
dp[0] = 1; dp[1] = 1;
for(int i = 2; i<=n; i++){
dp[i] = dp[i-1]+dp[i-2];
}
return dp[n];
}
}

377. Combination Sum IV 70. Climbing Stairs的更多相关文章

  1. LC 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  3. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  4. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. 377. Combination Sum IV

    问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...

  6. Leetcode 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. 377. Combination Sum IV——DP本质:针对结果的迭代,dp[ans] <= dp[ans-i] & dp[i] 找三者关系 思考问题的维度+1,除了数据集迭代还有考虑结果

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  8. 377. Combination Sum IV 返回符合目标和的组数

    [抄题]: Given an integer array with all positive numbers and no duplicates, find the number of possibl ...

  9. 377 Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. Flowerpot(又是尺取。。)

    题目:http://172.21.85.56/oj/exercise/problem?problem_id=21568 题目大意:老板需要你帮忙浇花.给出N滴水的坐标,y表示水滴的高度,x表示它下落到 ...

  2. Xshell添加快捷按钮

    1.打开xshell,点击[查看],勾[快速命令]: 2.点击xshell右下角[三],选择[添加按钮],在弹出框的“标签栏”和“文本”栏分别输入名称和命令,最后点击[确定]即可.

  3. 修改jupyter notebook的默认浏览器

    1.打开命令行 2.输入jupyter notebook --generate-config 3.显示出jupyter_notebook_config.py 文件所在的目录.按文件目录找到这个文件. ...

  4. yii1的后台分页和列表

    控制器: public function actionIndex(){ $model = new Cases('search'); $model->unsetAttributes(); // c ...

  5. 【ACM】Knapsack without repetition - 01背包问题

    无界背包中的状态及状态方程已经不适用于01背包问题,那么我们来比较这两个问题的不同之处,无界背包问题中同一物品可以使用多次,而01背包问题中一个背包仅可使用一次,区别就在这里.我们将 K(ω)改为 K ...

  6. indexOf 可用于字符串和数组

    indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. indexOf 与String类似,Array也可以通过indexOf()来搜索一个指定的元素的位置: var arr = ...

  7. python 时间序列resample参数

  8. 浅谈jrebel

    有个同事提高个jrebel的工具,提起tomcat的热部署方案. jrebel是一款收费的JVM级的热部署工具包. JVM级的热部署也就是说,可以不重启JVM,让修改或添加的类加载到JVM中. 加载器 ...

  9. Murano Weekly Meeting 2015.10.20

    Meeting time: 2015.October.20th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting sum ...

  10. (转)企业级NFS网络文件共享服务

    企业级NFS网络文件共享服务 原文:http://www.cnblogs.com/chensiqiqi/archive/2017/03/10/6530859.html --本教学笔记是本人学习和工作生 ...