daily leetcode - longest-consecutive-sequence - !
题目地址 https://leetcode.com/problems/longest-consecutive-sequence/ 题目描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O( n ) complexity. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. 思路 这道题要求求最长连续序列,并给定了 O(n)复杂度限制,我们的思路是,使用一个集合 HashSet 存入所有的数字,然后遍历数组中的每个数字,如果其在集合中存在,那么将其移除,然后分别用两个变量 pre 和 next 算出其前一个数跟后一个数,然后在集合中循环查....