题目

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.

分析

给定一个整形数组,求出最长的连续序列。例如数组[100,4,200,1,3,2],最长的连续序列长度为[1,2,3,4],长度为4。要求时间复杂度为O(n)。

首先,想到的办法就是 排序—>得到最长连续长度;但是不符合要求的时间复杂度,所以不予采用。

其次,第二个方法是利用c++中的set,直接会排序,并且没有重合的,但是set背后实现的原理牵扯到红黑树,时间复杂度同样不满足。

所以,解决该问题还是应该从哈希入手,建立hash索引,把查找的元素周围的都访问个遍,求出个临时最大值跟全局最大值比较。当再次访问该段的元素的时候,直接跳过。这样保证时间复杂度为O(n)。

c++11中数据结构为unordered_set,保证查找元素的时间复杂度为O(1),但是奇怪的是我才用的unordered_set并没有通过,给出的结果是TLE,于是,我就改用了unordered_map最后通过测试。

AC代码

class Solution {
public:
//方法一:使用unordered_set
int longestConsecutive1(vector<int>& nums) {
if (nums.empty() || nums.size() == 1)
return nums.size(); //保存序列元素个数
int size = nums.size();
unordered_set<int> existNums, visitedNums;
for (int i = 0; i < size; ++i)
{
existNums.insert(nums[i]);
} int maxCount = 0;
for (int i = 0; i < size; ++i)
{
//每个连续序列中的元素只需要遍历一次,只有不在已遍历序列中时,向两侧探查
if (visitedNums.find(nums[i]) != visitedNums.end())
continue;
int count = 1, less = nums[i] - 1, great = nums[i] + 1;
while (existNums.find(less) != existNums.end())
{
visitedNums.insert(less);
++count;
--less;
}//while
while (existNums.find(great) != existNums.end())
{
visitedNums.insert(great);
++count;
--less;
}//while
if (count > maxCount)
maxCount = count;
}//for
return maxCount;
} //方法二:使用unordered_map
int longestConsecutive(vector<int>& nums) {
if (nums.empty() || nums.size() == 1)
return nums.size(); //保存序列元素个数
int size = nums.size();
unordered_map<int, bool> visitedNums;
for (int i = 0; i < size; ++i)
{
visitedNums[nums[i]] = false;
} int maxCount = 0;
for (int i = 0; i < size; ++i)
{
//若已访问过,则跳过
if (visitedNums[nums[i]])
continue;
int count = 1;
//改变访问标记
visitedNums[nums[i]] = true; int less = nums[i] - 1, great = nums[i] + 1;
while (visitedNums.find(less) != visitedNums.end())
{
visitedNums[less] = true;
--less;
++count;
}//while while (visitedNums.find(great) != visitedNums.end())
{
visitedNums[great] = true;
++great;
++count;
}//while
if (count > maxCount)
maxCount = count;
}//for
return maxCount;
} };

GitHub测试程序源码

LeetCode(128) Longest Consecutive Sequence的更多相关文章

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

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  2. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  3. LeetCode 549. Binary Tree Longest Consecutive Sequence II

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...

  4. LeetCode 298. Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  5. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  6. LeetCode (32) Longest Valid Parentheses

    题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  7. LeetCode(5)Longest Palindromic Substring

    题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

  8. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  9. LeetCode(14)Longest Common Prefix

    题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...

随机推荐

  1. NET Core中使用Irony

    在.NET Core中使用Irony实现自己的查询语言语法解析器   在之前<在ASP.NET Core中使用Apworks快速开发数据服务>一文的评论部分,.NET大神张善友为我提了个建 ...

  2. 浅析libuv源码-node事件轮询解析(3)

    好像博客有观众,那每一篇都画个图吧! 本节简图如下. 上一篇其实啥也没讲,不过node本身就是这么复杂,走流程就要走全套.就像曾经看webpack源码,读了300行代码最后就为了取package.js ...

  3. 原创 html动态表格

    <table id="opttb"> <asp:Repeater ID="tempOptions" runat="server&qu ...

  4. IO(字节流、字符流)

      第1章 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流OutputStream ...

  5. ABAP数据类型

    数据类型表: 类型缩写 类型 默认长度 允许长度 初始值 描述 C 文本型 1   Space 字符串数据,如'Program' D 日期型 8 8 '00000000' 日期数据,格式为YYYYMM ...

  6. 带你零基础入门redis【二】

    本篇文章介绍redis如何设置开机自启动以及如何在java中应用 一.设置redis开机自启 1.修改redis配置 [root@VM_6_102_centos ~]# vim /usr/local/ ...

  7. .Net平台互操作技术:03. 技术验证

    上面两篇文章分别介绍了.Net平台互操作技术面临的问题,并重点介绍了通过P/Invoke调用Native C++类库的技术实现.光说不做是假把式,本文笔者将设计实验来证明P/Invoke调用技术的可行 ...

  8. c++中三种继承方式的区别

    public公有继承 protected保护继承 private私有继承 我们知道类的private和protected成员,在类外是不可以使用的.只有public成员可以在类外直接使用. 公有继承时 ...

  9. GIT新手入门学习教程

    廖雪峰的GIT教程 链接地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

  10. Leet-code144. Binary Tree Preorder Traversal

    这是一道将二叉树先序遍历,题目不难. 首先采用深搜递归 /** * Definition for a binary tree node. * public class TreeNode { * int ...