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的更多相关文章

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

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

  2. LeetCode: Longest Consecutive Sequence 解题报告

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

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

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

  4. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

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

  5. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  6. 128. Longest Consecutive Sequence(leetcode)

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

  7. [LintCode] Longest Consecutive Sequence 求最长连续序列

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

  8. LeetCode Binary Tree Longest Consecutive Sequence

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

  9. 24. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

随机推荐

  1. C#缓存处理

    第一种方式: 在ASP.NET中页面缓存的使用方法非常的简单,只需要在aspx页的顶部加这样一句声明即可: <%@ OutputCache Duration="60" Var ...

  2. sql - 选出指定范围的行

    Select no=Identity(int,1,1),* Into #temptable From dbo.tName order by fName --利用Identity函数生成记录序号 Sel ...

  3. Oracle学习【语句查询】

    基本查询语句any和all不能单独使用,必须和比较符一起使用>any 大于最小的例如:select * from emp where sal >any(1000,2000);<any ...

  4. Java之网络请求工具类(依赖:org.apache.http;注:HttpClient 4.4,HttpCore 4.4)

    到此处可以去下载依赖包:http://hc.apache.org/downloads.cgi import java.util.List; import org.apache.http.HttpSta ...

  5. shell命令:echo命令详解

    功能说明:显示文字. 语 法:echo [-ne][字符串] / echo [--help][--version] 补充说明:echo会将输入的字符串送往标准输出.输出的字符串间以空白字符隔开, 并在 ...

  6. Hibernate 使用说明

    Eclipse中hibernate连接mySQL数据库练习(采用的是hibernate中XML配置方式连接数据库,以后在更新其他方式的连接) Hibernate就是Java后台数据库持久层的框架,也是 ...

  7. 移动端下拉刷新、加载更多插件dropload.js(基于jQuery/Zepto)

    移动端下拉刷新.加载更多插件dropload.js(基于jQuery/Zepto) 原文:http://www.grycheng.com/?p=1869 废话不多说,先让大家看一下案例效果: DEMO ...

  8. SEMAT[软件工程方法和理论 Software Engineering Method and Theory]

    Agile software development Agile software development is a group of software development methods bas ...

  9. 上传XML文件字符编码问题

    1.上传的XML文件的空格的字符编码和倒入到数据库的空格的字符编码不是一种编码格式,导致导入到数据库的数据和XML文件的数据不一致的情况,进而使展示到界面上的数据在进行搜索时不能搜索出来.解决办法: ...

  10. 业界良心:Square开源Viewfinder,25万行代码全公布!

    http://www.csdn.net/article/2014-05-06/2819633-square-open-sources-viewfinder https://github.com/vie ...