From 78efc0ab870f97f015a7babefad86092624889aa Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Mon, 4 Oct 2021 09:55:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200279=20=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0=20JavaScript=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 首先 0 不是题目中所说的完全平方数,所有 i 的范围应该从 1 开始; 其次,i <= n 不太合理,增加了大量无用计算,应改成 i**2 (即 i^2)<= n 更为合适 --- problems/0279.完全平方数.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index a00ed47e..b1af7e95 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -334,8 +334,8 @@ var numSquares1 = function(n) { let dp = new Array(n + 1).fill(Infinity) dp[0] = 0 - for(let i = 0; i <= n; i++) { - let val = i * i + for(let i = 1; i**2 <= n; i++) { + let val = i**2 for(let j = val; j <= n; j++) { dp[j] = Math.min(dp[j], dp[j - val] + 1) }