編輯JAVA解答的部分內容

1. 提供JAVA填充數組的函數Arrays.fill()
2. for loop中不需要if statement,並有用 // 解釋原因
This commit is contained in:
Lozakaka
2023-06-01 19:44:43 -04:00
committed by GitHub
parent 6156804822
commit 32185d5f2d

View File

@ -177,15 +177,19 @@ class Solution {
for (int j = 0; j <= n; j++) {
dp[j] = max;
}
//如果不想要寫for-loop填充數組的話也可以用JAVA內建的Arrays.fill()函數。
//Arrays.fill(dp, Integer.MAX_VALUE);
//当和为0时组合的个数为0
dp[0] = 0;
// 遍历物品
for (int i = 1; i * i <= n; i++) {
// 遍历背包
for (int j = i * i; j <= n; j++) {
if (dp[j - i * i] != max) {
//if (dp[j - i * i] != max) {
dp[j] = Math.min(dp[j], dp[j - i * i] + 1);
}
//}
//不需要這個if statement因爲在完全平方數這一題不會有"湊不成"的狀況發生( 一定可以用"1"來組成任何一個n故comment掉這個if statement。
}
}
return dp[n];