【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前后访问,可以把包含当前元素的最大连同序列都找出来;而且访问过的元素不用再访问。

=======================================================

第二次过这道题,大体的思路能顺下来,但是疏忽了几个细节。AC代码如下:

复制代码
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
            int global_longest = 1;
            // initialize map
            unordered_map<int, bool> visited;
            for ( int i=0; i<nums.size(); ++i ) visited[nums[i]]=false;
            // go one traversal
            for ( int i=0; i<nums.size(); ++i )
            {
                if ( visited[nums[i]] ) continue;
                visited[nums[i]] = true;
                int local_longest = 1;
                int curr = nums[i]-1;
                // search towards smaller direction
                while (    visited.find(curr)!=visited.end() )
                {
                    local_longest++;
                    visited[curr] = true;
                    curr--;
                }
                // search towards larger direction
                curr = nums[i]+1;
                while ( visited.find(curr)!=visited.end() )
                {
                    local_longest++;
                    visited[curr] = true;
                    curr++;
                }
                // update global longest
                global_longest = std::max(global_longest, local_longest);
            }
            return global_longest;
    }
};
复制代码

tips:

1. 根据某个元素往‘前’、‘后’两个方向遍历之前,要先记得把该元素设置为访问过(即,visited[nums[i]] = true;)

2. 第一遍把while循环中的 visited[curr]=true都写成了visited[nums[i]],这个纯属低级错误,不要再犯

3. 第一遍有一个逻辑上的错误:

  "for ( int i=0; i<nums.size() && !visited[nums[i]]; ++i )"

本意是跳过已经访问过的元素。。。但是这么写如果遇到了访问过的元素,就不是跳过了,而是直接退出循环了。这是个思维的陷阱,记下来,下次不要再犯。

posted on   承续缘  阅读(168)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示