longest-consecutive-sequence
2016-06-29 20:47
381 查看
import java.util.*;
public class Solution {
public int longestConsecutive(int[] num) {
Set<Integer> set =new HashSet<Integer>();
for(int i=0;i<=num.length-1;i++)
{
set.add(num[i]);
}
int max=1;
for(int e:num)
{
int left=e-1;
int right=e+1;
int count=1;
while(set.contains(left))
{
count++;
set.remove(left);
left--;
}
while(set.contains(right))
{
count++;
set.remove(right);
right++;
}
max=Math.max(max,count);
}
return max;
}
}
public class Solution {
public int longestConsecutive(int[] num) {
Set<Integer> set =new HashSet<Integer>();
for(int i=0;i<=num.length-1;i++)
{
set.add(num[i]);
}
int max=1;
for(int e:num)
{
int left=e-1;
int right=e+1;
int count=1;
while(set.contains(left))
{
count++;
set.remove(left);
left--;
}
while(set.contains(right))
{
count++;
set.remove(right);
right++;
}
max=Math.max(max,count);
}
return max;
}
}
相关文章推荐
- [置顶] Android平台一款UI体验好于NumberPicker的自定义控件NumberPickerView
- UITextView限制最大输入长度(UITextField相同)
- 关于google的GsonBuilder中的2个设置
- LeetCode - 98. Validate Binary Search Tree
- android AIDL 编译错误,finished with non-zero exit value 1
- SICP 练习2.40 unique-pairs
- UIScrollView的delaysContentTouches与canCencelContentTouches属性
- Double类parseDouble和valueOf方法的区别
- "break";"continue";"标签名+:"的运用
- UITableView Cell 弹簧动画效果
- sequelize (二)
- 资源推荐:FootLOL: Epic Fail League
- 快速排序QuickSort
- iOS的UITextField光标位置右移的简单实现方法
- 优先队列(priority queue)
- UGUI组件获取及属性设置方法
- EasyUI 之 下拉框默认选择第一个选项
- UITableView使用中的一些刁专问题总结
- android 漂亮的UI集合库
- 低功耗蓝牙的连接过程