碧桂園房地產(chǎn)最新消息北京網(wǎng)站優(yōu)化多少錢(qián)
提示:文章寫(xiě)完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔
文章目錄
- 前言
- 一、力扣70. 爬樓梯
- 二、力扣322. 零錢(qián)兌換
- 三、力扣279. 完全平方數(shù)
前言
一、力扣70. 爬樓梯
class Solution {public int climbStairs(int n) {int[] dp = new int[n+1];dp[0] = 1;for(int i = 0; i <= n; i ++){for(int j = 1; j <= 2; j ++){if(i >= j){dp[i] += dp[i-j];}}}return dp[n];}
}
二、力扣322. 零錢(qián)兌換
class Solution {public int coinChange(int[] coins, int amount) {int[] dp = new int[amount+1];for(int i = 1; i < dp.length; i ++){dp[i] = Integer.MAX_VALUE;}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], dp[j-coins[i]] + 1);}}}return dp[amount] == Integer.MAX_VALUE ?-1:dp[amount];}
}
三、力扣279. 完全平方數(shù)
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];}
}