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.

两种方法:1. 利用 hash_map 结构,数组有序时查找的思想。

class Solution {
public:
int longestConsecutive(vector<int> &num) {
if(num.size() == 0) return 0;
unordered_map<int, bool> _map;
for(size_t i = 0; i < num.size(); ++i)
_map.insert(pair<int ,bool>(num[i], false));
int ans = 0;
for(auto it = _map.begin(); it != _map.end(); ++it) {
if(it->second) continue; // visited
int len = 1;
int v1 = it->first-1;
while(_map.count(v1)) { _map[v1--] = true; len++; }
int v2 = it->first+1;
while(_map.count(v2)) { _map[v2++] = true; len++; }
if(len > ans) ans = len;
}
return ans;
}
};

2. 动态的构造线段(两端为线段始末位置),这样从一端点可获取另一端点位置。若当前点可增加有向线段长度,拓展线段。

class Solution {
public:
int longestConsecutive(vector<int> &num) {
int answer = 0;
unordered_map<int ,int>_map;
int low, high;
for(int i = 0; i < num.size(); ++i) {
if(_map.count(num[i])) continue; // must be used.
_map[num[i]] = num[i];
low = high = num[i];
if(_map.count(num[i]-1)) low = _map[num[i]-1];
if(_map.count(num[i]+1)) high = _map[num[i]+1];
answer = max(answer, high - low + 1);
if(low == high) continue;// not in a line
_map[low] = high;
_map[high] = low;
}
return answer;
}
};

24. Longest Consecutive Sequence的更多相关文章

  1. [LeetCode] 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] Longest Consecutive Sequence 求最长连续序列

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

  3. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  4. 128. Longest Consecutive Sequence(leetcode)

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

  5. [LintCode] Longest Consecutive Sequence 求最长连续序列

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

  6. LeetCode Binary Tree Longest Consecutive Sequence

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

  7. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  8. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

  9. 298. Binary Tree Longest Consecutive Sequence

    题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...

随机推荐

  1. ASP.NET Web API 入门示例详解

    REST服务已经成为最新的服务端开发趋势,ASP.NET Web API即为.NET平台的一种轻量级REST架构. ASP.NET Web API直接借鉴了ASP.NET MVC的设计,两者具有非常类 ...

  2. jQuery.ajax() 函数详解

    jQuery.ajax()函数用于通过后台HTTP请求加载远程数据. jQuery.ajax()函数是jQuery封装的AJAX技术实现,通过该函数,我们无需刷新当前页面即可获取远程服务器上的数据. ...

  3. python import其他文件夹下的模块

    模块的路径不在默认搜索路径中,需要在sys.path中添加 import syssys.path.append('需要模块的文件夹路径')

  4. MVC中的自定义控件——分页

    上一篇是个简单例子,这篇借分页写个升级版的例子. 不想拼html代码,因为不好修改,那写一个PartialView. @model System.Web.Mvc.PagerModel @if (Mod ...

  5. readfile() 函数

    定义和用法 readfile() 函数输出一个文件. 该函数读入一个文件并写入到输出缓冲. 若成功,则返回从文件中读入的字节数.若失败,则返回 false.您可以通过 @readfile() 形式调用 ...

  6. 黑马程序员:Java编程_异常

    =========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== 异常即对问题的描述,将问题进行对象的封装. 1. 异常体系:Throwable ...

  7. Xcode与OX 版本对照表

    xcode1.0-xcode2.x 版本 OS X运行版本 OS X SDK(s) 1.0 OS X Panther(10.3.x) OS X Puma(10.1.x),OS X Jaguar(10. ...

  8. Neuroaesthetics神经美学

    欢迎您到脑科学的世界! 神经美学(或neuroaesthetics)是一个相对较新的经验主义美学的子学科.经验主义美学需要科学的方法来研究艺术和音乐的审美观念. neuroesthetics于2002 ...

  9. DAO接口及实现类

    DAO接口中定义了所有的用户操作,如添加记录.删除记录及查询记录. package chapter13; import java.util.*; public interface UserDAO { ...

  10. VS 2013 打包程序教程

    简述 如果你只是想要在他人的机子上运行你的程序而不想安装,有一种简单的方法,只要使用本教程的“步骤—3.生成Release 文件夹”即可.但是有一点需要注意,如果你在程序中调用了其他的dll,那么你需 ...