From 718d3c065a351547f1c95a1941372b40e36e07e8 Mon Sep 17 00:00:00 2001 From: SteveL <66228787+stevenleon99@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:33:01 -0400 Subject: [PATCH] =?UTF-8?q?Update=200018.=E5=9B=9B=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add 剪枝 to the second for loop --- problems/0018.四数之和.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 9c8bb4fe..8e34713d 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -262,6 +262,11 @@ class Solution { for (int j = i + 1; j < nums.length; j++) { + // nums[i]+nums[j] > target 直接返回, 剪枝操作 + if (nums[i]+nums[j] > 0 && nums[i]+nums[j] > target) { + return result; + } + if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重 continue; }