|
1 | 1 | [#0128-longest-consecutive-sequence] |
2 | | -= 128. Longest Consecutive Sequence |
| 2 | += 128. 最长连续序列 |
3 | 3 |
|
4 | | -{leetcode}/problems/longest-consecutive-sequence/[LeetCode - Longest Consecutive Sequence^] |
| 4 | +https://leetcode.cn/problems/longest-consecutive-sequence/[LeetCode - 128. 最长连续序列 ^] |
5 | 5 |
|
6 | | -Given an unsorted array of integers, find the length of the longest consecutive elements sequence. |
| 6 | +给定一个未排序的整数数组 `nums`,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。 |
7 | 7 |
|
8 | | -Your algorithm should run in O(_n_) complexity. |
| 8 | +请你设计并实现时间复杂度为 stem:[O(n)] 的算法解决此问题。 |
9 | 9 |
|
10 | | -*Example:* |
| 10 | +*示例 1:* |
| 11 | + |
| 12 | +.... |
| 13 | +输入:nums = [100,4,200,1,3,2] |
| 14 | +输出:4 |
| 15 | +解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。 |
| 16 | +.... |
| 17 | + |
| 18 | +*示例 2:* |
| 19 | + |
| 20 | +.... |
| 21 | +输入:nums = [0,3,7,2,5,8,4,6,0,1] |
| 22 | +输出:9 |
| 23 | +.... |
| 24 | + |
| 25 | +*提示:* |
| 26 | + |
| 27 | +* `0 \<= nums.length \<= 10^5^` |
| 28 | +* `-10^9^ \<= nums[i] \<= 10^9^` |
11 | 29 |
|
12 | | -[subs="verbatim,quotes,macros"] |
13 | | ----- |
14 | | -*Input:* [100, 4, 200, 1, 3, 2] |
15 | | -*Output:* 4 |
16 | | -*Explanation:* The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4. |
17 | | ----- |
18 | 30 |
|
| 31 | +== 思路分析 |
19 | 32 |
|
| 33 | +首先想到的思路是排序,然后再查找。 |
| 34 | + |
| 35 | +答案的思路是:先将数字都添加到集合,然后遍历即可,只有没有前一位数字时,开始统计个数。 |
| 36 | + |
| 37 | +看其他选手答案,使用并查集也可以解决。 |
20 | 38 |
|
21 | 39 | [[src-0128]] |
| 40 | +[tabs] |
| 41 | +==== |
| 42 | +一刷:: |
| 43 | ++ |
| 44 | +-- |
22 | 45 | [{java_src_attr}] |
23 | 46 | ---- |
24 | 47 | include::{sourcedir}/_0128_LongestConsecutiveSequence.java[tag=answer] |
25 | 48 | ---- |
| 49 | +-- |
| 50 | +
|
| 51 | +// 二刷:: |
| 52 | +// + |
| 53 | +// -- |
| 54 | +// [{java_src_attr}] |
| 55 | +// ---- |
| 56 | +// include::{sourcedir}/_0128_LongestConsecutiveSequence_2.java[tag=answer] |
| 57 | +// ---- |
| 58 | +// -- |
| 59 | +==== |
| 60 | + |
| 61 | + |
| 62 | +== 参考资料 |
| 63 | + |
| 64 | +. https://leetcode.cn/problems/longest-consecutive-sequence/solutions/276931/zui-chang-lian-xu-xu-lie-by-leetcode-solution/[128. 最长连续序列 - 官方题解^] |
| 65 | +. https://leetcode.cn/problems/longest-consecutive-sequence/solutions/3652806/jian-yi-dai-ma-cbing-cha-ji-qiu-jie-by-w-b4hj/[128. 最长连续序列 - 简易代码——C++并查集求解^] |
| 66 | +. https://leetcode.cn/problems/longest-consecutive-sequence/solutions/3005726/ha-xi-biao-on-zuo-fa-pythonjavacgojsrust-whop/[128. 最长连续序列 - 哈希表 O(n) 做法^] |
26 | 67 |
|
0 commit comments