From d412f035eb25c5b3a24cf197834453b94d8f4d20 Mon Sep 17 00:00:00 2001 From: neco Date: Sat, 18 Jul 2026 14:13:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原代码在重复元素下会 O(n²) 超时,改为遍历 Set 修复 for (int num : nums) -> for (int num : set) --- ...25\277\350\277\236\347\273\255\345\272\217\345\210\227.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/2-\345\223\210\345\270\214/3-0129-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.md" "b/2-\345\223\210\345\270\214/3-0129-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.md" index 6f0ec44..26e7050 100644 --- "a/2-\345\223\210\345\270\214/3-0129-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.md" +++ "b/2-\345\223\210\345\270\214/3-0129-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.md" @@ -123,7 +123,7 @@ public int longestConsecutive(int[] nums) { int maxLength = 0; // 遍历每个数字 - for (int num : nums) { + for (int num : set) { // 如果num-1存在,说明num不是序列的起点,跳过 if (!set.contains(num - 1)) { int currentNum = num; @@ -170,4 +170,4 @@ public int longestConsecutive(int[] nums) { --- 作者:忍者算法 -公众号:忍者算法 \ No newline at end of file +公众号:忍者算法