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.


题解:首先构建一个hashmap,键值为num中的所有元素,这样查找一个元素是否在num中就只需要O(1)的时间了。

接下来以上面给定的例子来说明算法:

比如上述num数组中第一个元素是100,那么就找101或者99在不在num中,发现不在;

num中第二个元素是4,那么就找3或者5是否在num中,发现3在num中,而5不在;于是继续找2在不在num中,然后找1在不在num中.....最终知道有4,3,2,1这么一个长度为4的序列。在这个遍历过程中,因为已经找到了序列4,3,2,1,所以如果以后遍历到3的时候再找到的序列3,2,1已经没有意义了(肯定比当前的最长序列短),所以3,2,1这三个元素在未来的遍历中,我们都不需要考虑了,这里就把它们在hashmap中对应的值改成1,作为标志。

num中第三个元素是200,那么就找199或者201是否在num中,发现都不在;

num中还有元素1,3,2,基于上述描述的原因,就不再遍历了。

这样就保证了每个元素最多遍历一次,时间复杂度为O(n)。

示例图如下;

代码如下:

 public class Solution {
public int longestConsecutive(int[] num) {
int answer = 0;
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>(); for(int i:num)
map.put(i, 0); for(int i:num){
if(map.get(i) == 1)
continue; int currMax = 1;
int tmp = i-1;
//left
while(map.containsKey(tmp)){
map.put(tmp, 1); //we have visited tmp,so we don't start with tmp in the future
currMax++;
tmp--;
} tmp = i+1;
while(map.containsKey(tmp)){
map.put(tmp, 1);
currMax++;
tmp++;
} if(currMax > answer)
answer = currMax;
}
return answer;
}
}

【leetcode刷题笔记】Longest Consecutive Sequence的更多相关文章

  1. 【leetcode刷题笔记】Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  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] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

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

  4. LeetCode 549. Binary Tree Longest Consecutive Sequence II

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

  5. LeetCode 298. Binary Tree Longest Consecutive Sequence

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

  6. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  7. 算法题丨Longest Consecutive Sequence

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

  8. [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 ...

  9. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

随机推荐

  1. 关于iphone自动播放音频和视频问题的解决办法

    大家都知道 做移动端 会遇到音频和视频无法自动播放问题(我也遇到了)于是想办法解决这个问题 我只是找到了在微信中解决的办法(如果谁有在别的浏览器有这个办法  请私聊我 )我是没有发现 document ...

  2. Eclipse搭建C++开发环境

    http://jingyan.baidu.com/article/456c463b67b4310a59314468.html

  3. 安装Hadoop 1.1.2 (二 安装配置SSH)

    1 查找SSH  yum search ssh 2 如果没有安装, yum install openssh.x86_64 4 直接运行  ssh-keygen -t dsa -P '' -f /roo ...

  4. node.js实现国标GB28181流媒体点播(即实时预览)服务解决方案

    背景 28181协议全称为GB/T28181<安全防范视频监控联网系统信息传输.交换.控制技术要求>,是由公安部科技信息化局提出,由全国安全防范报警系统标准化技术委员会(SAC/TC100 ...

  5. zabbix server 端安装

    1.系统环境 [root@crazy-acong ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@crazy-acong ~] ...

  6. 【翻译自mos文章】在重建控制文件之前应该考虑的事情

    在重建控制文件之前应该考虑的事情 来源于: Things to Consider Before Recreating the Controlfile (文档 ID 1475632.1) 适用于: Or ...

  7. Django导出excel中文乱码解决方案

    Django官方文档有关于怎么生成csv文件的方法 import csv from django.http import HttpResponse def some_view(request): # ...

  8. [iOS]通过JS调用iOS函数时的URL编码问题

    在前面的文章:[iOS]在WebApp中怎样使用JS调用iOS的函数 中,提到了怎样使用JS通过改动URL调用iOS的内部函数. 当中会遇到一个问题,就是编码问题.比方通过URL调用弹窗,在里面写上内 ...

  9. activiti基础--2----------------------(流程定义)

    Deployment 部署对象 1.一次部署的多个文件信息,对于不需要的流程可以删除和修改 2.对应的表 act_re_deployment #部署对象表 act_re_procdef #流程定义表 ...

  10. basic--factory

    <?php /* 工厂模式: 由工厂类根据参数来决定创建出哪一种产品类的实例: 工厂类是指包含了一个专门用来创建其他对象的方法的类.所谓按需分配,传入参数进行选择,返回具体的类.工厂模式的最主要 ...