一、零钱兑换II(LeetCode518)
class Solution {
public int change(int amount, int[] coins) {
int[] dp = new int[amount + 1];
dp[0] = 1;
for(int i = 0;i < coins.length;i++){
for(int j = coins[i];j <= amount;j++){
dp[j] += dp[j - coins[i]];
}
}
return dp[amount];
}
}
二、组合总和IV(LeetCode377)
class Solution {
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target + 1];
dp[0] = 1;
for (int i = 0; i <= target; i++) {
for (int j = 0; j < nums.length; j++) {
if (i >= nums[j]) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
}
三、爬楼梯(LeetCode70)
class Solution {
public int climbStairs(int n) {
int[] dp = new int[n + 1];
int[] weight = {1,2};
dp[0] = 1;
for (int i = 0; i <= n; i++) {
for (int j = 0; j < weight.length; j++) {
if (i >= weight[j]) {
dp[i] += dp[i - weight[j]];
}
}
}
return dp[n];
}
}
四、零钱兑换(LeetCode322)
class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
for (int j = 0; j < dp.length; j++) {
dp[j] = Integer.MAX_VALUE;
}
dp[0] = 0;
for (int i = 0; i < coins.length; i++) {
for (int j = coins[i]; j <= amount; j++) {
if(dp[j - coins[i]] != Integer.MAX_VALUE){
dp[j] = Math.min(dp[j - coins[i]] + 1,dp[j]);
}
}
}
if(dp[amount] == Integer.MAX_VALUE) return -1;
return dp[amount];
}
}
五、完全平方数(LeetCode279)
class Solution {
public int numSquares(int n) {
int[] dp = new int[n + 1];
for(int j = 0;j <= n;j++){
dp[j] = Integer.MAX_VALUE;
}
dp[0] = 0;
for(int i = 1;i * i <= n;i++){
for(int j = i * i;j <= n;j++){
dp[j] = Math.min(dp[j - i * i] + 1,dp[j]);
}
}
return dp[n];
}
}