LeetCode_Disjoint-Set_Longest Consecutive Sequence

128. Longest Consecutive Sequence


1. 问题描述:

输入一个未排序的整型数组,找到最长的连续元素序列,并返回其长度。

2. 解决思路:

拿到这道题,首先想到是先排序,然后遍历找到连续最长的元素。但是我们想通过更好的方式去解决这个问题。所以想到用HashMap存储每个节点的值和这个节点所在序列的长度。当遍历到每个元素时,找当前Map中是否有这个元素的左右连续元素存在,然后计算当前节点所在序列的长度,与最长长度比较替换。

3. java代码:

public class Solution {
    public int longestConsecutive(int[] nums) {
        if(nums.length==0)
            return 0;

        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();

        int longestLength = 0;
        for(int i=0;i<nums.length;i++) {

            if(!map.containsKey(nums[i])) {
                int leftLength = map.containsKey(nums[i]-1)? map.get(nums[i]-1) : 0;
                int rightLength = map.containsKey(nums[i]+1)? map.get(nums[i]+1) : 0;

                int curLength = leftLength + 1 + rightLength;

                map.put(nums[i],curLength);

                map.put(nums[i]-leftLength,curLength);
                map.put(nums[i]+rightLength,curLength);

                if(curLength>longestLength)
                    longestLength = curLength;
            }
        }
        return longestLength;
    }
}

4. 算法评估:



希望多多指正交流

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 07:06:52

LeetCode_Disjoint-Set_Longest Consecutive Sequence的相关文章

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

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

Longest Consecutive Sequence 数组连续数字的情况

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

【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

【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 in

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your fu

Binary Tree Longest Consecutive Sequence Leetcode

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

Longest Consecutive Sequence Total Accepted: 19169 Total Submissions: 68303My Submissions 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 consec

[LeetCode] Longest Consecutive Sequence(DP)

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 in

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