题目:

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.

题解:

这道题利用HashSet的唯一性解决,能使时间复杂度达到O(n)。首先先把所有num值放入HashSet,然后遍历整个数组,如果HashSet中存在该值,就先向下找到边界,找的同时把找到的值一个一个从set中删去,然后再向上找边界,同样要把找到的值都从set中删掉。所以每个元素最多会被遍历两边,时间复杂度为O(n)。

代码如下:

 1     public int longestConsecutive(int[] num) {  
 2         if(num == null||num.length == 0)
 3             return 0;
 4         
 5         HashSet<Integer> hs = new HashSet<Integer>();  
 6         
 7         for (int i = 0 ;i<num.length; i++)   
 8             hs.add(num[i]);  
 9          
         int max = 0;  
         for(int i=0; i<num.length; i++){  
             if(hs.contains(num[i])){
                 int count = 1;  
                 hs.remove(num[i]);
                 
                 int low = num[i] - 1; 
                 while(hs.contains(low)){  
                     hs.remove(low);  
                     low--;  
                     count++;  
                 }
                 
                 int high = num[i] + 1;  
                 while(hs.contains(high)){  
                     hs.remove(high);  
                     high++;  
                     count++;  
                 }  
                 max = Math.max(max, count);  
             }  
         }  
         return max;  
     } 

Longest Consecutive Sequence leetcode java的更多相关文章

  1. 128. Longest Consecutive Sequence(leetcode)

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

  2. Binary Tree Longest Consecutive Sequence -- LeetCode

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

  3. Longest Consecutive Sequence [LeetCode]

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

  4. Longest Consecutive Sequence——Leetcode

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

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

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

  6. LeetCode Binary Tree Longest Consecutive Sequence

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

  7. LeetCode 549. Binary Tree Longest Consecutive Sequence II

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

  8. LeetCode 298. Binary Tree Longest Consecutive Sequence

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

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

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

随机推荐

  1. iuap

    2017.12 用友今年着力点往云平台发展,是时候整理一下思路 第一:iuap 第二:Linux 第三:财务会计业务入门 第四:NC节点视频教程--财务模块 2019年3月4日 all in iuap ...

  2. Spring Boot 结合 Redis 缓存

    Redis官网: 中:http://www.redis.cn/ 外:https://redis.io/ redis下载和安装 Redis官方并没有提供Redis的Windows版本,这里使用微软提供的 ...

  3. 利用过滤器对string类型的入参进行统一trim

    背景 最近做的一些项目都是后台管理系统,主要是对表单数据的增删改查操作,其中有些表单项是字符串类型的,对于这些类型的表单项就需要在保存或编辑之前要进行.trim()处理,刚开始感觉没什么,遇到了就手动 ...

  4. Music in Car CF 746F

    题目:http://codeforces.com/problemset/problem/746/F 先感叹一下题目之长! 一些测试样例在后面给出. 题目大意: Sasha 去工作的路上喜欢听歌,途中经 ...

  5. HTML基础-DAY1

    HTML基础 Web的本质就是利用浏览器访问socket服务端,socket服务端收到请求回复数据提供给浏览器进行渲染显示. import socket def main(): sock = sock ...

  6. 1015 Reversible Primes (20)(20 point(s))

    problem A reversible prime in any number system is a prime whose "reverse" in that number ...

  7. 机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价

    python3学习使用api 线性回归,和 随机参数回归 git: https://github.com/linyi0604/MachineLearning from sklearn.datasets ...

  8. django定时任务

    1.celery流程图: Celery的架构由三部分组成,消息中间件(message broker),任务执行单元(worker)和任务执行结果存储(task result store)组成 2.使用 ...

  9. [UOJ#245][UER#7]天路(近似算法)

    允许5%的相对误差,意味着我们可以只输出$\log_{1.05} V$种取值并保证答案合法.并且注意到答案随着区间长度而单增,故取值不同的答案区间是$O(\log_{1.05} V)$的. 于是初始x ...

  10. [Arc058E] Iroha and Haiku

    [Arc058E] Iroha and Haiku 题目大意 问有多少\(n\)个数的正整数序列,每个数在\([1,10]\)之间,满足存在\(x,y,z,w\)使得\(x\to y-1,y\to z ...