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. Codeforces 316C2 棋盘模型

    Let's move from initial matrix to the bipartite graph. The matrix elements (i, j) for which i + j ar ...

  2. Jenkins自动化CI CD流水线之3--参数化构建

    一. 背景 如果只是简单的构建,jenkins自己默认的插件可以做,但是如果我们想要在构建过程中有更多功能,比如说:选择性构建.传参.项目指定变量等等其他功能,基础的参数化构建可以实现一些简单功能,但 ...

  3. 2.Servlet(一)

    1.Servlet的编写.访问过程: (1)编写部署Servlet程序: 编写源文件->编译类文件->部署程序->运行->Servlet处理请求,返回响应. (2)Eclips ...

  4. Vue 中怎么发起请求(二)

    fetch 是新一代XMLHttpRequest的一种替代方案.无需安装其他库.可以在浏览器中直接提供其提供的api轻松与后台进行数据交互. 基本用法: 1 fetch(url,{parmas}).t ...

  5. my06_sysbench install for mysql 并初始化表数据

    sysbench安装 ************************************************************** 安装sysbench依赖包 rpm -q autom ...

  6. mapreduce统计总数

    现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1. buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t ...

  7. 数据结构---Java---有序数组

    [自定义有序数组] 查找算法: 线性查找算法:依次对比查询: 二分查找算法:必须是有序: insert:要插入的value与数组中每个元素进行比较,当有值>value时,此处的index之后的元 ...

  8. Linux添加、创建新用户

    给Linux添加新用户,新建用户,新建帐号 添加用户组 sudo groupadd groupname 添加用户 sudo useradd username -m -s /bin/bash -d /h ...

  9. Animation 把动画片段拖入Animation组件里后不能播放

    1. 选中动画片段,点击右上角三道横杠那个按钮,选择Debug: 2.选择Legacy,然后再点击那个三道横岗的按钮,选择Normal就可以用了应该.

  10. ubuntu14.04通过 gvm 安装 go语言开发环境

    最近用回了ubuntu ,所以打算安装golang学习当下比较火热的这个语言 本来打算使用 sudo apt-get install golang的 安装后发现 是1.2.1不是最新版 所以上网上搜了 ...