8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 93db5a0 commit 5e68f30Copy full SHA for 5e68f30
problems/0279.完全平方数.md
@@ -214,8 +214,26 @@ class Solution:
214
return dp[n]
215
```
216
217
+Python3:
218
+```python
219
+class Solution:
220
+ def numSquares(self, n: int) -> int:
221
+ # 初始化
222
+ # 组成和的完全平方数的最多个数,就是只用1构成
223
+ # 因此,dp[i] = i
224
+ dp = [i for i in range(n + 1)]
225
+ # dp[0] = 0 无意义,只是为了方便记录特殊情况:
226
+ # n本身就是完全平方数,dp[n] = min(dp[n], dp[n - n] + 1) = 1
227
+
228
+ for i in range(1, n): # 遍历物品
229
+ if i * i > n:
230
+ break
231
+ num = i * i
232
+ for j in range(num, n + 1): # 遍历背包
233
+ dp[j] = min(dp[j], dp[j - num] + 1)
234
-
235
+ return dp[n]
236
+```
237
238
Go:
239
```go
0 commit comments