377. 组合总和 Ⅳ
题目
链接
代码
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[dp.length-1];
}
}
57. 爬楼梯(第八期模拟笔试)
题目
链接
代码
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] dp = new int[n+1];
dp[0] = 1;
for(int j=0;j<dp.length;j++){
for(int i =1;i<=m;i++){
if(j>=i){
dp[j] += dp[j-i];
}
}
}
System.out.println(dp[n]);
}
}
322. 零钱兑换
题目
链接
代码
class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount+1];
Arrays.fill(dp,Integer.MAX_VALUE);
dp[0] = 0;
for(int i =0;i<coins.length;i++){
for(int j = coins[i];j<=amount;j++){
dp[j] = (int) Math.min(Long.valueOf(dp[j]),dp[j-coins[i]]+1L);
}
}
return dp[amount]==Integer.MAX_VALUE?-1:dp[amount];
}
}
279. 完全平方数
题目
链接
代码
class Solution {
public int numSquares(int n) {
int[] dp = new int[n+1];
Arrays.fill(dp,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],dp[j-i*i]+1);
}
}
return dp[n];
}
}