Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd2dfc4

Browse files
Update 0279.完全平方数.md
添加 python3 版本代码
1 parent 2c24d81 commit fd2dfc4

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

‎problems/0279.完全平方数.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
输入:n = 13
2727
输出:2
2828
解释:13 = 4 + 9
29-
29+
3030
提示:
3131
* 1 <= n <= 10^4
3232

@@ -184,6 +184,38 @@ class Solution {
184184

185185
Python:
186186

187+
```python3
188+
class Solution:
189+
def numSquares(self, n: int) -> int:
190+
'''版本一'''
191+
# 初始化
192+
nums = [i**2 for i in range(1, n + 1) if i**2 <= n]
193+
dp = [10**4]*(n + 1)
194+
dp[0] = 0
195+
# 遍历背包
196+
for j in range(1, n + 1):
197+
# 遍历物品
198+
for num in nums:
199+
if j >= num:
200+
dp[j] = min(dp[j], dp[j - num] + 1)
201+
return dp[n]
202+
203+
def numSquares1(self, n: int) -> int:
204+
'''版本二'''
205+
# 初始化
206+
nums = [i**2 for i in range(1, n + 1) if i**2 <= n]
207+
dp = [10**4]*(n + 1)
208+
dp[0] = 0
209+
# 遍历物品
210+
for num in nums:
211+
# 遍历背包
212+
for j in range(num, n + 1)
213+
dp[j] = min(dp[j], dp[j - num] + 1)
214+
return dp[n]
215+
```
216+
217+
218+
187219

188220
Go:
189221
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /