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 ...
随机推荐
- 小白零基础C#学习笔记
一.概述 1..Net 1)..Net平台 2)..Net Frameword框架 说明:是.Net平台中不可缺少的一部分,提供了一个稳定的运行环境来保证.Net平台开发的各种应用能够正常运转. 2. ...
- java实现压缩文件下载
转载:https://www.cnblogs.com/zeng1994/p/7862288.html
- logback日志简记
%date{HH:mm:ss.SSS} [%thread] %-5level %logger{20}:%line - %msg%n 输出: 09:54:09.657 [main] INFO c.e. ...
- 苏D_8M150
20161226 麦德龙 西边 弄堂 前车 转弯 刹车,我 刹车,下雨路滑,滑到.没撞到前车.车型号没看...只记了车牌... 右手撑了一下,肩膀估计是撑伤了,举起来 比较疼... 不知 该如何处理, ...
- 移动测试之appium+python 入门代码(二)
ps: 对于环境安装可能会碰到各种问题,还是要一一解决. 执行: appium-doctor 显示上边界面说明,环境已完成. 同时将手机连接主机(用数据线) ^_^ 执行 adb devices 显示 ...
- Matrix Chain Multiplication (堆栈)
题目链接:https://vjudge.net/problem/UVA-442 题目大意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.如果乘法无法进行,输出error. 假定A是m*n的矩 ...
- Unity [SerializeField]
在Unity3d中Unity3D 中提供了非常方便的功能可以帮助用户将 成员变量 在Inspector中显示,并且定义Serialize关系. 也就是说凡是显示在Inspector 中的属性都同时具有 ...
- UI3D转2D平面显示UI3DTo2D血条显示
UI3D转2D平面显示UI3DTo2D血条显示: using UnityEngine; using System.Collections; public class UI3DTo2D : MonoBe ...
- java多线程异常捕获
java多线程中出现了异常,如何捕获.利用UncaughtExceptionHandler这个接口就可以了. 代码如下: package com.ming.thread.six.threadcreat ...
- SpringBoot如何集成Jedis
添加jedis依赖 在项目pom.xml文件中添加依赖 <!-- 添加jedis依赖 --> <dependency> <groupId>redis.clients ...