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 ...
随机推荐
- <script runat=server>、<%%>和<%#%>的区别
①<script runat="server">代码段与<%%>内联代码段的区别 在asp.net页面的aspx文件中允许使用<script runa ...
- 数据库性能高校:CPU使用过高(上)
CPU使用率过高问题很容易被发现,但是诊断却不是很容易.CPU使用过高很多时候会成为其它问题的替罪羊,所以在确认和故障诊断时要抽丝剥茧. 调查CPU压力 三个主要的工具:性能监视器,SQLTrace, ...
- C#程序将对象保存为json文件的方法
首先,从NuGet上下载JSON .Net,安装到所需项目中. 对象obj保存到文件的步骤: 1. 创建文件 // 获取当前程序所在路径,并将要创建的文件命名为info.json string fp ...
- PHP设计模式之:工厂模式
工厂模式: 由工厂类根据参数来决定创建出哪一种产品类的实例: 工厂类是指包含了一个专门用来创建其他对象的方法的类.所谓按需分配,传入参数进行选择,返回具体的类.工厂模式的最主要作用就是对象创建的封装. ...
- 关于正则表达式的转义 PHP
如正则的函数 preg_replace($patern, $replacement, $content) 等等 其中如果 $content 中要替换 \ 成 /,必须在 $patern中写成 \\\\ ...
- 计算机视觉的matlab工具箱及MVG等
MATLAB Functions for Multiple View Geometry Peter Kovesi's Matlab functions for Computer Vision Jean ...
- log4j详细配置说明
log4j配置祥解 第一步:加入log4j-1.2.8.jar到lib下.第二步:在CLASSPATH下建立log4j.properties.内容如下:1 log4j.rootCategory=INF ...
- VS2013相关资料
visual studio 主页 http://msdn.microsoft.com/en-us/vstudio/aa718325.aspx vs2013 download http://www.mi ...
- Delphi中快捷键的使用
CTRL+SHIFT+↑(↓) 在过程.函数.事件内部, 可跳跃到相应的过程.函数.事件的定义(在INTERFACE和IMPLEMENTATION之间来回切换)CTRL+J (弹出DELPHI语句提示 ...
- GHOST还原
整理日: 2015年2月16日 GHOST GHO2Disk STEP01 STEP02 STEP03 STEP04 STEP05 STEP06 STEP07