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 ...
随机推荐
- [DB2]实现项目多数据库切换(上)--环境部署
基本软硬件信息:Windows 8.1 X64 / Microsoft Visual Studio 2012 / ThinkPad S3-S431 安装工具:IBM Data Studio 4.1. ...
- Mybatis的学习总结:mybatis的入门介绍
一.myBatis简述 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- 三步走起 提升 iOS 审核通过率 下篇
根据2015年的数据统计情况,并结合<苹果应用商店审核指南>,互娱 iOS 预审组通过细分将预审工作划为3大模块:客户端资源检查.应用内容检查和提审资源检查. 在上一篇文章中,Bugly ...
- #pragma——push and pop
#pragma warning(push) #pragma warning(pop) 这两个语句在#include <SDKDDKVer.h>头文件中出现,处于好奇,查看msdn文档有了进 ...
- Java单例模式深入详解
原文地址:http://www.cnblogs.com/hxsyl/ 仅作为笔记收藏…… 一.问题引入 偶然想想到的如果把Java的构造方法弄成private,那里面的成员属性是不是只有通过stati ...
- cookie : 存储数据
cookie : 存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来像访问者电脑上存储数据 1.不同的浏览器存放的cookie位置不一样,也是不能通用的 2.cookie的存储 ...
- Listview控件实现已选择效果
Winform中用Listview控件实现更新点击选择后已选择效果,如图: 代码如下: private void frmSelect_Load(object sender, EventArgs e) ...
- oracle sqlldr控制文件模板
Sqlldr userid=lgone/tiger control=a.ctl LOAD DATA INFILE 't.dat' // 要导入的文件 // INFILE 'tt.date' // 导入 ...
- jquery返回顶部-ie6配合css表达式。
css: .wraper{ width:980px; height:2048px; margin:0 auto; background:#ccc; } /*返回头部*/ #backToTop a{;; ...
- 配置并学习微信JS-SDK(3)—菜单接口
1.设置菜单 //2.批量显示菜单项接口 wx.showMenuItems({ menuList: [ 'menuItem:profile', //查看公众号 'menuItem: ...