LeetCode 128 最长连续序列
有勇气的牛排
23
算法
2025-08-05 22:06:06
LeetCode 128 最长连续序列
题目
给定一个未排序的整数数组 nums
,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
请你设计并实现时间复杂度为 0(n) 的算法解决此问题。
示例 1:
输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
示例 2:
输入:nums = [0,3,7,2,5,8,4,6,0,1]
输出:9
示例 3:
输入:nums = [1,0,1,2]
输出:3
提示:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
排序法 O(n log n)
思路:
- 先去重,再排序。
- 遍历排序后的数组,统计连续数字的长度。
优点:逻辑简单,容易实现。
缺点:复杂度 O(n log n),不符合题目 O(n) 要求。
def longestConsecutive_sort(nums):
nums = sorted(set(nums))
longest = 1
cur_len = 1
for i in range(1, len(nums)):
if nums[i] == nums[i - 1] + 1:
cur_len += 1
else:
longest = max(longest, cur_len)
cur_len = 1
return max(longest, cur_len)
nums = [100, 4, 200, 1, 3, 2]
print(longestConsecutive_sort(nums))
nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1]
print(longestConsecutive_sort(nums))
nums = [1, 0, 1, 2]
print(longestConsecutive_sort(nums))
哈希法+起点判断 O(n)
核心思想:
- 用
set
存储 所有数字。
- 只从连续序列起点开始向后找,起点条件:
num-1
不在集合中。
- 从起点往后查找连续数字的长度。
时间复杂度:O(n)
空间复杂度:O(n)
def longestConsecutive_sort(nums):
nums_set = set(nums)
longest = 0
for num in nums_set:
if num - 1 not in nums_set:
cur_num = num
cur_len = 1
while cur_num + 1 in nums_set:
cur_num += 1
cur_len += 1
longest = max(longest, cur_len)
return longest
<h1><a id="LeetCode_128__0"></a>LeetCode 128 最长连续序列</h1>
<h2><a id="_2"></a>题目</h2>
<p>给定一个未排序的整数数组 <code>nums</code>,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。</p>
<p>请你设计并实现时间复杂度为 0(n) 的算法解决此问题。</p>
<p><strong>示例 1:</strong></p>
<pre><code class="lang-">输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
</code></pre>
<p><strong>示例 2:</strong></p>
<pre><code class="lang-">输入:nums = [0,3,7,2,5,8,4,6,0,1]
输出:9
</code></pre>
<p><strong>示例 3:</strong></p>
<pre><code class="lang-">输入:nums = [1,0,1,2]
输出:3
</code></pre>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= nums.length <= 105</code></li>
<li><code>-109 <= nums[i] <= 109</code></li>
</ul>
<h2><a id="_On_log_n_35"></a>排序法 O(n log n)</h2>
<p><strong>思路</strong>:</p>
<ol>
<li>先去重,再排序。</li>
<li>遍历排序后的数组,统计连续数字的长度。</li>
</ol>
<p><strong>优点</strong>:逻辑简单,容易实现。</p>
<p><strong>缺点</strong>:复杂度 O(n log n),不符合题目 O(n) 要求。</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">longestConsecutive_sort</span>(<span class="hljs-params">nums</span>):
nums = <span class="hljs-built_in">sorted</span>(<span class="hljs-built_in">set</span>(nums))
<span class="hljs-comment"># print(nums)</span>
longest = <span class="hljs-number">1</span>
cur_len = <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>, <span class="hljs-built_in">len</span>(nums)):
<span class="hljs-comment"># print(i, nums[i], nums[i - 1])</span>
<span class="hljs-keyword">if</span> nums[i] == nums[i - <span class="hljs-number">1</span>] + <span class="hljs-number">1</span>:
<span class="hljs-comment"># 计算 当前序列长度</span>
cur_len += <span class="hljs-number">1</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-comment"># 序列断开,取出最大的,重新开始</span>
longest = <span class="hljs-built_in">max</span>(longest, cur_len)
cur_len = <span class="hljs-number">1</span>
<span class="hljs-comment"># 最终取最大序列长度</span>
<span class="hljs-keyword">return</span> <span class="hljs-built_in">max</span>(longest, cur_len)
nums = [<span class="hljs-number">100</span>, <span class="hljs-number">4</span>, <span class="hljs-number">200</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>]
<span class="hljs-built_in">print</span>(longestConsecutive_sort(nums))
nums = [<span class="hljs-number">0</span>, <span class="hljs-number">3</span>, <span class="hljs-number">7</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>, <span class="hljs-number">8</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>]
<span class="hljs-built_in">print</span>(longestConsecutive_sort(nums))
nums = [<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
<span class="hljs-built_in">print</span>(longestConsecutive_sort(nums))
</code></div></pre>
<h2><a id="_On_80"></a>哈希法+起点判断 O(n)</h2>
<p><strong>核心思想</strong>:</p>
<ul>
<li>用 <code>set</code> 存储 所有数字。</li>
<li>只从连续序列起点开始向后找,起点条件:<code>num-1</code> 不在集合中。</li>
<li>从起点往后查找连续数字的长度。</li>
</ul>
<p><strong>时间复杂度</strong>:O(n)<br />
<strong>空间复杂度</strong>:O(n)</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">longestConsecutive_sort</span>(<span class="hljs-params">nums</span>):
nums_set = <span class="hljs-built_in">set</span>(nums)
longest = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> nums_set:
<span class="hljs-comment"># 只从起点开始</span>
<span class="hljs-keyword">if</span> num - <span class="hljs-number">1</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> nums_set:
cur_num = num
cur_len = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> cur_num + <span class="hljs-number">1</span> <span class="hljs-keyword">in</span> nums_set:
cur_num += <span class="hljs-number">1</span>
cur_len += <span class="hljs-number">1</span>
longest = <span class="hljs-built_in">max</span>(longest, cur_len)
<span class="hljs-keyword">return</span> longest
</code></div></pre>
评论区