【Longest Consecutive Sequence】cpp

题目

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 O(n) complexity.

代码

class Solution {
public:
    int longestConsecutive(vector<int> &num) {
        // hashmap record if element in num is visited
        std::map<int,bool> visited;
        for(std::vector<int>::iterator i = num.begin(); i != num.end(); ++i)
        {
            visited[*i] = false;
        }
        // search the longest consecutive
        unsigned int longest_global = 0;
        for(std::vector<int>::iterator i = num.begin(); i != num.end(); ++i)
        {
            if(visited[*i]) continue;
            unsigned int longest_local = 0;
            for(int j = *i+1; visited.find(j) != visited.end(); ++j)
            {
                visited[j] = true;
                ++longest_local;
            }
            for(int j = *i-1; visited.find(j) != visited.end(); --j)
            {
                visited[j] = true;
                ++longest_local;
            }
            longest_global = std::max(longest_global, longest_local+1);
        }
        return longest_global;
    }
};

Tips:

1. 要想O(n), 而且无序,只能结合hashmap

2. 这里需要明确的一个逻辑是,通过hashmap前后访问,可以把包含当前元素的最大连同序列都找出来;而且访问过的元素不用再访问。

时间: 2024-10-14 09:58:13

【Longest Consecutive Sequence】cpp的相关文章

【Longest Valid Parentheses】cpp

题目: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example

【Longest Common Prefix】cpp

题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class Solution { public: string longestCommonPrefix(vector<string>& strs) { if ( strs.size()==0 ) return ""; std::string tmp_str = strs[0];

【Longest Palindromic Substring】cpp

题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 代码: class Solution { public: string longestPalindrome(string s) { const

【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】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 OJ】Longest Consecutive Sequence

Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classical problem where we can reduce the running time by the help of hash table. By given a list of numbers, we can find the longest consecutive sequence b

【哈希表】Longest Consecutive Sequence

题目: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 i

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