[LeetCode] 128. 最长连续序列

题目链接 : https://leetcode-cn.com/problems/longest-consecutive-sequence/

题目描述:

给定一个未排序的整数数组,找出最长连续序列的长度。

要求算法的时间复杂度为 O(n)

示例:

输入: [100, 4, 200, 1, 3, 2]
输出: 4
解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。

思路:

这道题, 难在时间复杂度限定在\(O(n)\), 要不排序就可以了!

思路一:集合

集合,查询时间复杂度为\(O(1)\)

思路二:字典

遍历数组, 用字典(哈希)记录目前与该值可以组成最长连续序列.

直接看代码(有注释)

代码:

思路一:

class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        nums = set(nums)
        res = 0
        for num in nums:
            # 判断是否是第一个数字
            if num - 1 not in nums:
                tmp = 1
                while num + 1 in nums:
                    num += 1
                    tmp += 1
                res = max(res, tmp)
        return res

java

class Solution {
    public int longestConsecutive(int[] nums) {
        Set<Integer> num_set = new HashSet<>();
        for (int n : nums) num_set.add(n);
        int res = 0;
        for (int num : num_set) {
            if (!num_set.contains(num - 1)) {
                int tmp = 1;
                while (num_set.contains(num + 1)) {
                    tmp++;
                    num++;
                }
                res = Math.max(res, tmp);
            }
        }
        return res;
    }
}

思路二:

class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        # 记录首尾值的最长长度
        lookup = {}
        res = 0
        for num in nums:
            if num not in lookup:
                # 判断左右是否可以连起来
                left = lookup[num - 1] if num - 1 in lookup else 0
                right = lookup[num + 1] if num + 1 in lookup else 0
                # 记录长度
                lookup[num] = left + right + 1
                # 把头尾都设置为最长长度
                lookup[num - left] = left + right + 1
                lookup[num + right] = left + right + 1
                res = max(res, left + right + 1)
        return res

java

class Solution {
    public int longestConsecutive(int[] nums) {
        HashMap<Integer, Integer> lookup = new HashMap<>();
        int res = 0;
        for (int num : nums) {
            if (!lookup.containsKey(num)) {
                // 查看左右两边是否可以相连
                int left = (lookup.containsKey(num - 1)) ? lookup.get(num - 1) : 0;
                int right = (lookup.containsKey(num + 1)) ? lookup.get(num + 1) : 0;
                lookup.put(num, left + right + 1);
                // 改变首尾两个长度(换成更长的长度)
                lookup.put(num - left, left + right + 1);
                lookup.put(num + right, left + right + 1);
                res = Math.max(res, left + right + 1);
            }

        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/powercai/p/11181681.html

时间: 2024-08-17 20:04:20

[LeetCode] 128. 最长连续序列的相关文章

图解leetcode —— 128. 最长连续序列

前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2]输出: 4解释: 最长连续序列是 [1, 2, 3, 4].它的长度为 4. 思路: 首先,我们先来看一个简单的例子:序列123 序列 56,插入4,求连续序列长度. 很容易得出结论,答案是6,那么这个6是怎么来的呢?6 = len(1,2

LeetCode 128. 最长连续序列(Longest Consecutive Sequence)

题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, 3, 4].它的长度为 4. 解题思路 利用并查集的思想,构造一个map记录数组中以每个数所在的最长连续序列长度.每次遍历到一个数时,首先检查map中是否存在该数,若存在直接跳过,否则作如下更新操作: 找到左右相邻数字是否在map中,若存在则分别记录他们所在的最长连续序列长度,并更新当前的

【LeetCode】128. 最长连续序列

题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, 4].它的长度为4 思路 思路一 先由小到大进行排序 考虑三种情况: 前后相差1,则是连续序列 前后相等,循环continue 最后一个元素,break 代码 def longestConsecutive(nums) -> int: ????if nums: ????????nums.sort()

128. 最长连续序列

给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, 3, 4].它的长度为 4. class Solution(object): def longestConsecutive(self, num): """ :type nums: List[int] :rtype: int """ if num is

leetcode 128. 最长连续子序列

题目描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 即最长的连续序列为 [1,2,3,4] 思路分析: 由于要求O(n)的复杂读,因此直接排序是不可行的. 这里用到的是并查集的思想.对于每一个数,去查小于1和大于1的数是否在序列中.通过迭代求解. 尽管在 for 循环中嵌套了一个 while 循环,时间复杂度看起来像是二次方级别的.但其实它是线性的算法.因为只有当 current

[LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

[LeetCode] Longest Consecutive Sequence 求最长连续序列

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

Leetcode 最长连续序列

题目链接:https://leetcode-cn.com/problems/longest-consecutive-sequence/ 题目大意: 略. 分析: 注意有重复值,序列为空等情况. 代码如下: 1 class Solution { 2 public: 3 int longestConsecutive(vector<int>& nums) { 4 unordered_map< int, int > m1; // 存以 key 为首的最长连续序列的长度 5 unor

GEEK编程练习— —最长连续序列

题目 给定一个无序的整数数组,返回最长连续序列的长度.要求时间复杂度为O(n). 输入 [100, 4, 200, 1, 3, 2, 0, -1] 输出 6 分析 因为要求时间负责度为O(n),所以不能先排序再查找.所以想到查询最快的hash表,记录每个元素是否使用,对每个元素,往左右扩张,直到不连续为止. 代码 #include <iostream> #include <unordered_map> #include <algorithm> using namespa