From 33dcb8dbd3d98ad38bdb37e0d7b6a6113ded4422 Mon Sep 17 00:00:00 2001 From: Yang Date: Wed, 9 Jun 2021 15:46:57 -0400 Subject: [PATCH] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Java code: initialize dp array with length `nums.length` is sufficient --- problems/0198.打家劫舍.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index f49857bb..63a68c36 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -118,7 +118,7 @@ class Solution { if (nums == null || nums.length == 0) return 0; if (nums.length == 1) return nums[0]; - int[] dp = new int[nums.length + 1]; + int[] dp = new int[nums.length]; dp[0] = nums[0]; dp[1] = Math.max(dp[0], nums[1]); for (int i = 2; i < nums.length; i++) {