377. Combination Sum IV 70. Climbing Stairs
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的更多相关文章
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 377. Combination Sum IV
问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...
- Leetcode 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 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 ...
- 377. Combination Sum IV 返回符合目标和的组数
[抄题]: Given an integer array with all positive numbers and no duplicates, find the number of possibl ...
- 377 Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- 江西财经大学第一届程序设计竞赛 D
链接:https://www.nowcoder.com/acm/contest/115/D来源:牛客网 题目描述 事情,是这样的. 有这么一天双休日的中午. 我刚把我衣服扔进了洗衣机,然后拿了个小板凳 ...
- ymPrompt.js消息提示组件
转载:https://www.cnblogs.com/linzheng/archive/2010/11/15/1878058.html 使用说明: 1.在页面中引入ymPrompt.js.如:< ...
- eclipse安装阿里规范模板
https://github.com/alibaba/p3c/tree/master/p3c-formatter 1.代码模板(含注释等) 2.代码格式化
- 10-----BBS论坛
BBS论坛(十) 10.1.客户端权限验证功能完成 (1)cms/cms_profile 显示当前用户的角色和权限 <tr> <td>角色:</td> <td ...
- C# Linq 查询数据库(DataSet)生成 Tree
效果图如下 cs代码 using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- Django-1 简介
1.1 MVC与MTV模型 MVCWeb服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负 ...
- C++的静态Static
类的静态数据成员是属于类(即与类关联)而不属于类的每个对象(不与类的每个对象关联)(相当于该静态对象在所有的类对象中共享.),所以初始化方法与一般的变量不同,需要在类的构造函数之外进行初始化. 类的静 ...
- Animation 把动画片段拖入Animation组件里后不能播放
1. 选中动画片段,点击右上角三道横杠那个按钮,选择Debug: 2.选择Legacy,然后再点击那个三道横岗的按钮,选择Normal就可以用了应该.
- netbeans 窗体字体大小设置
当计算机分辨率变大的时候,打开netbeans的时候,字体就会变得越来越小 看起来很不爽,所要就要改变一下,窗体字体大小. 在网上找到了一段修改netbeans窗体字体大小的配置信息,现标记起来,以便 ...
- eureka的一点细节
第二部分粗略的过一遍,还是有些模糊,再来相对系统的看一下: ---------------------------------------------------------------------- ...