70. 爬楼梯
题目链接:70. 爬楼梯 - 力扣(LeetCode)
class Solution {public int climbStairs(int n) {int[] dp = new int[n+1];if(n <= 2) {return n;}dp[0] = 1;for(int i = 1; i < n + 1; ++i) {for(int j = 1; j < 3; ++j) {if(i < j) {break;}dp[i] += dp[i-j];}}return dp[n];}
}
使用完全背包做的爬楼梯
322. 零钱兑换
题目链接:322. 零钱兑换 - 力扣(LeetCode)
class Solution {public int coinChange(int[] coins, int amount) {if(amount == 0) {return 0;}int[] dp = new int[amount + 1];for(int i = 0; i < coins.length; ++i) {for(int j = coins[i]; j < amount + 1; ++j) {if(dp[j-coins[i]] != 0 || j - coins[i] == 0) {dp[j] = dp[j] != 0 ? Math.min(dp[j], dp[j-coins[i]] + 1) : dp[j-coins[i]] + 1;}}}return dp[amount] == 0 ? -1 : dp[amount];}
}
279.完全平方数
题目链接:279. 完全平方数 - 力扣(LeetCode)
class Solution {public int numSquares(int n) {double x = Math.pow(n, 0.5);if(x % 1 == 0) {return 1;}int a = (int)x;int[] dp = new int[n + 1];for(int i = 1; i < a + 1; ++i) {for(int j = i * i; j < n + 1; ++j) {if(dp[j - i*i] != 0 || j - i*i == 0) {dp[j] = dp[j] == 0 ? dp[j - i * i] + 1 : Math.min(dp[j], dp[j-i*i]+1);}}}return dp[n] == 0 ? -1 : dp[n];}
}