Longest Consecutive Sequence hashset
public class Solution { public int longestConsecutive(int[] num) {
HashSet<Integer> hash=new HashSet<Integer>();
int max=1;
for(int n:num)
{
hash.add(n);
}
int j=0;
while(!hash.isEmpty()&&j<num.length)
{
// if(!hash.contains(num[j])) continue;
int count=1; int k=num[j]+1;
while(hash.contains(k))
{
hash.remove(k);
k=k+1;
count++; }
k=num[j]-1;
while(hash.contains(k))
{
hash.remove(k);
k=k-1;
count++;
}
hash.remove(num[j]); if(count>max) max=count;
j++; } return max; }
}
http://www.programcreek.com/2013/01/leetcode-longest-consecutive-sequence-java/
Longest Consecutive Sequence hashset的更多相关文章
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LintCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 24. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- javascript类继承系列二(原型链)
原型链是采用最主要的继承方式,原理:每一个类(构造器,js中的function)都有一个原型属性(prototype)指向一个原型对象,原型对象有一个构造器(constructor),它又指回到fun ...
- SQL内外左右交叉连接
什么是连接查询? 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据. 目的:实现多个表查询操作. 一般是用作关联两张或两张以上的数据表时用的.看起来有点抽象,我们举个例子,做两张表:学生表( ...
- 深入理解shared pool共享池之library cache系列二
背景 继续上文:深入理解shared pool共享池之library cache系列一,学习library cache数据结构,本文主要学习library cache object(lco)的数据结构 ...
- SQL2005 学习笔记 窗口函数(OVER)【转】
1.简介: SQL Server 2005中的窗口函数帮助你迅速查看不同级别的聚合,通过它可以非常方便地累计总数.移动平均值.以及执行其它计算. 窗口函数功能非常强大,使用起来也十分容易.可以使用这个 ...
- FXBlurView用法
FXBlurView是UIView的子类,它实现毛玻璃效果的原理其实就是覆盖上一层FXBlurView的实例对象. - (void)viewDidLoad { [super viewDidLoad]; ...
- C++专题 - 面向对象总结
1. 什么是类?什么是对象?对象与类的关系是什么? 答:类就是相同的数据和相同的一组对象的集合,即类是对具有相同数据结构和相同操作的一类对象的描述: 对象是描述其属性的数据以及对这些数 ...
- C语言基础程序设计
1 概论 程序(指令和数据的集合)在运行时,首先会被加载到内存(此时称为进程),然后由CPU通过控制器的译码从内存中读取指令,并按照指令的要求,从存储器中取出数据进行指定的运算和逻辑操作等加工,然后再 ...
- Android Studio中JNI -- 2 -- 编写c文件
继上一篇,我们在native接口中编写了2个方法 生成的相应.h文件 这时,需要我们自己去完善.c文件 /* DO NOT EDIT THIS FILE - it is machine generat ...
- [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法
最近把一个项目的jQuery升级到最新版,发现有些页面报错Cannot read property ‘msie’ of undefined.上jQuery网站上搜了一下,原因是$.browser这个a ...
- winform C#屏幕右下角弹出消息框并自动消失
①弹出信息框后慢慢下降消失 在主窗体中新增按钮重命名为btnShowAndDisappearMessages,在click事件中写如下代码: private void btnShowAndDisapp ...