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 ...
随机推荐
- 网页Gzip
网页Gzip压缩检测工具 网站Gzip压缩可以减小服务器带宽占用,提高用户打开网页速度,最多可以提升网站80%的性能,是每个网站必须开启的功能, 站长工具网页 Gzip压缩检测工具方便站长朋友们检测特 ...
- [访问系统] C#计算机信息类ComputerInfo (转载)
下载整个包,只下载现有类是不起作用的 http://www.sufeinet.com/thread-303-1-1.html 点击此处下载 using System; using System.Man ...
- .NET设计模式(6):原型模式(Prototype Pattern)
):原型模式(Prototype Pattern) ); //使用颜色 string colorName = "red"; C ...
- 新浪微博failed to receive access token
这个问题很多人都遇到了,很多人发邮件我,我之前解决的时候也花了很大的代价,发现很多的都是抄袭,然后就是找不到答案,确实比较痛苦.避免大家跟我范一样的错误. 保证几个东西: 1.签名正确---非常重要. ...
- SQL,学习基础2
列=字段, 记录=实体 事物日志文件(用来记录数据库的增删情况,扩展名LDF) 数据库文件(但是只有一个是主数据库文件(即用它来启动的),其余为次数据库文件)mdf 数据类型: 整形(整数)——in ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...
- line-height行高使用技巧
若父元素标签高度一定,假设为150px,子元素需要垂直居中,再重新给子元素设置一个行高就好了,省事省力
- jquery 之选择符
css:选择符$('#selected-plays > li') 使用了子元素组合符,查找 ID 为 selected-plays 的元素的子元素( > )中所有的列表 li$('#sel ...
- 求算符文法的FIRSTVT集的算法
原理 数据结构 G = {'key':[v1,v2,v3],'key':[v1,v2,v3]}; VN = []; Vt = []; FirstVT = {'key':[v1,v2,v3],'key' ...
- PC和单片机通过MODBUS RTU通信
最近研究了一下MODBUS通信,在STC12C5A60S2单片机上实现了MODBUS协议的部分功能,方便上位机从单片机系统上获取数据,比如由单片机获取的温度.湿度.或者控制信号的状态等.有了MODBU ...