【leetcode】Longest Consecutive Sequence
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.
以题目给出的数组为例:对于100,先向下查找99没找到,然后向上查找101也没找到,那么连续长度是1,从哈希表中删除100;然后是4,向下查找找到3,2,1,向上没有找到5,那么连续长度是4,从哈希表中删除4,3,2,1。这样对哈希表中已存在的某个元素向上和向下查找,直到哈希表为空。算法相当于遍历了一遍数组,然后再遍历了一遍哈希表,复杂的为O(n)
 class Solution {
 public:
     int longestConsecutive(vector<int> &num) {
         unordered_set<int> hash;
         unordered_set<int>::iterator it;
         for(int i=;i<num.size();i++) hash.insert(num[i]);
         int count;
         int result=;
         while(!hash.empty())
         {
             count=;
             it=hash.begin();
             int num0=*it;
             hash.erase(num0);
             int num=num0+;
             while(hash.find(num)!=hash.end())
             {
                 count++;
                 hash.erase(num);
                 num++;
             }
             num=num0-;
             while(hash.find(num)!=hash.end())
             {
                 count++;
                 hash.erase(num);
                 num--;
             }
             if(result<count) result=count;
         }
         return result;
     }
 };
【leetcode】Longest Consecutive Sequence的更多相关文章
- 【leetcode】Longest Consecutive Sequence(hard)☆
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
 - 【LeetCode-128】Longest Consecutive Sequence
		
描述 输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) . 输入 54,55,300,12,56 输出 3 通常我们看有没有连续序列时 找某个数有没有的前后的数,比如看到5 ...
 - 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
		
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
 - 【LeetCode OJ】Longest Consecutive Sequence
		
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
 - [LeetCode] 128. Longest Consecutive Sequence 解题思路
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
 - [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
 - 【leetcode刷题笔记】Longest Consecutive Sequence
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
 - Java for LeetCode 128 Longest Consecutive Sequence
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
 - 【LeetCode】Longest Palindromic Substring 解题报告
		
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
 
随机推荐
- [转]不再以讹传讹,GET和POST的真正区别
			
原文地址:http://www.nowamagic.net/librarys/veda/detail/1919 如果有人问你,GET和POST,有什么区别?你会如何回答? 我的经历 前几天有人问我这个 ...
 - @RestController注解下返回到jsp视图页面(转)(转)
			
这个问题我也遇到过,下面的方法可以试试 蓝萝卜blu @RestController注解下返回到jsp视图页面 spring4.1中添加了@RestController注解很方便,集成了@Respon ...
 - PowerDesigner导出的sql中去掉双引号,主要用于Oracle
			
如题,这些双引号对于Oracle建表实在是糟糕透了: 解决方法: (转载,作者的百度已经挂了,无法放上链接,自私一把,以前的Oracle项目不见了,无法展示) 1.去掉Oracle生成的SQL创建语句 ...
 - C#通过编程方式实现Ping
			
代码是照着书敲的,贴出来方便平时参考 using System; using System.Collections.Generic; using System.Linq; using System.T ...
 - ubuntu使用ssh登入不执行.bashrc解决方法
			
解决方法,可以直接输入 bash即可. 理解 bashrc 和 profile linux bashrc profile SEP 30TH, 2011 BY SUNTEYA 在一般的 linux 或者 ...
 - 如何在 Arch Linux 的终端里设定 WiFi 网络
			
如果你使用的是其他 Linux 发行版 而不是 Arch CLI,那么可能会不习惯在终端里设置 WiFi.尽管整个过程有点简单,不过我还是要讲一下.在这篇文章里,我将带领新手们通过一步步的设置向导,把 ...
 - 代码重构-3  用Tuple代替 out与ref
			
返回单一值是良好的编程习惯 原代码: public LotteryViewModel ValidateLottery(LotteryBaseData baseData, int authTime, o ...
 - php统计字数函数
			
function countWords($str){ echo (mb_strlen($str, 'utf8') + strlen($str))/2; }
 - C++_Eigen函数库用法笔记——The Array class and Coefficient-wise operations
			
The advantages of Array Addition and subtraction Array multiplication abs() & sqrt() Converting ...
 - 菜鸟学习Spring Web MVC之二
			
有文章从结构上详细讲解了Spring Web MVC,我个菜鸟就不引据来讲了.说说强悍的XP环境如何配置运行环境~~ 最后我配好的环境Tomcat.Spring Tool Suites.Maven目前 ...