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

如果时间复杂度没有要求的话思路很常见,先排序O(nlogn),然后从头遍历到尾,找到最长的连续序列就可以了。

但是这里的时间复杂度要求是O(n)

实现思路需要做一些改变:我们先定义一个map<int, int>,遍历一遍数组,将(key, value)存入map,key是数组中的每一个数,value是1。

接着,我们再遍历一遍数组,对于当前遍历的某个数 k,我们定义一个值 index,index从k开始不停自增1,如果每次自增1后 index 依然可以在map中找到值,就说明数组中存在k,k+1, k+2...这样的连续序列;接着,index 从k开始不停自减1,直到map里找不到这样的index,这样就找出了k-1, k-2, ...这样的连续序列。我们将两次计算找到的连续序列总长度len存储下来。

遍历到下一个数时,依旧这样做。最后找到len的最大值。

为了避免重复便利,map中已经访问过的key可以设置为-1,当我们遍历到数组中某一个值k时,如果map[k] == -1,说明k已经被计入过某一个连续序列了,因此不用继续计算。

因此,数组所有的元素都被访问两次,总时间复杂度为O(2n)。

代码:

class Solution {
public:
int longestConsecutive(vector<int> &num) {
if(num.size() == ) return ;
map<int, int> m;
vector<int>::iterator ite = num.begin();
vector<int>::iterator end = num.end();
for(; ite != end; ++ite){
if(m.find(*ite) == m.end())
m.insert(pair<int, int>(*ite, ));
}
int maxlen = , len = ;
for(ite = num.begin(); ite != end; ++ite){
if(m[*ite] > ){
int index = *ite;
len = ;
for(; m.find(index) != m.end(); ++len, m[index++] = -);
for(index = *ite-; m.find(index) != m.end(); ++len, m[index--] = -);
if(len > maxlen) maxlen = len;
}
}
return maxlen;
}
};

[LeetCode] 数组的最长连续数, O(n)解法的更多相关文章

  1. 【LeetCode】128. 最长连续序列

    题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, ...

  2. 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  4. [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  5. LeetCode 最长连续递增序列

    给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...

  6. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...

  7. 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...

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

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

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

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

随机推荐

  1. ServiceStack.Ormlit 使用Insert的时候自增列不会被赋值

    Insert签名是这样的,将第2个参数设置为true就会返回刚插入的自增列ID了,然后可以手工赋值到对象上面去 public static long Insert<T>(this IDbC ...

  2. [leetcode-666-Path Sum IV]

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  3. 自测之Lesson15:TCP&UDP网络编程

    题目:编写一个TCP通信的程序. 实现代码: #include <stdio.h> #include <sys/socket.h> #include <unistd.h& ...

  4. TensorFlow安装解惑

    本文整理自网络,若有侵犯请告知. 1.安装环境 目前TensorFlow社区推荐的环境是Ubuntu, 但是TensorFlow同时支持Mac,Windows上的安装部署. 2.关于GPU版本 因为深 ...

  5. jsp文件中charset和pageEncoding的区别

    jsp文件中charset和pageEncoding的区别:  contentType的charset是指服务器发送给客户端时的内容编码,contentType里的charset=utf-8是指示页面 ...

  6. (转)apktool+dex2jar+jd_gui

    转:http://www.cnblogs.com/MichaelGuan/archive/2011/10/25/2224578.html apktool: 可以解析资源文件,比如布局文件xml等,方便 ...

  7. 转 Js 跨域CORS报错 Response for preflight has invalid HTTP status code 405

    转自:http://www.cnblogs.com/SilenceTom/p/6697484.html 调用接口遇到Response for preflight has invalid HTTP st ...

  8. C# HttpWebRequest post提交数据,提交对象

    1.客户端方法 //属于客户端 //要向URL Post的方法 public void PostResponse() { HttpWebRequest req = (HttpWebRequest)Ht ...

  9. 【week3】四则运算 单元测试

    上一周的四则运算有bug,这次补充正确代码: // 中缀转后缀 public String[] SolveOrder(String[] in, HashMap<String, Integer&g ...

  10. C#常见函数

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...