LintCode : 最长连续序列
来源:互联网 发布:于莺离婚知乎 编辑:程序博客网 时间:2024/05/02 04:37
最长连续序列
- 描述
- 笔记
- 数据
- 评测
给定一个未排序的整数数组,找出最长连续序列的长度。
您在真实的面试中是否遇到过这个题?
Yes
说明
要求你的算法复杂度为O(n)
样例
给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3, 4],返回所求长度 4
标签
- 定义一个集合,去除了重复数,在每个数两边找,判断是否连续
public class Solution { /** * @param nums: A list of integers * @return an integer */ public int longestConsecutive(int[] num) { // write you code here if(num==null||num.length==0){ return 0; } Set<Integer> set = new HashSet<Integer>(); for(int i=0;i<num.length;i++){ set.add(num[i]); } int maxLongest = 0; for(int i=0;i<num.length;i++){ int down = num[i]-1; while(set.contains(down)){ set.remove(down); down--; } int up = num[i]+1; while(set.contains(up)){ set.remove(up); up++; } maxLongest = Math.max(maxLongest, up-down-1); } return maxLongest; }}
0 0
- lintcode ----最长连续序列
- LintCode:最长连续序列
- LintCode-最长连续序列
- LintCode : 最长连续序列
- LintCode-最长连续序列
- lintcode--最长连续序列
- 最长连续序列-LintCode
- lintcode-最长连续子序列
- LintCode--最长上升连续子序列
- lintcode-最长上升连续子序列-397
- 最长上升连续子序列(LintCode)
- LintCode- 最长上升连续子序列
- lintCode刷题--最长上升连续子序列
- LintCode:最长上升连续子序列
- *[Lintcode]Longest Consecutive Sequence最长连续序列
- lintcode -- 最长上升连续子序列
- lintcode:最长上升连续子序列
- LintCode: 最长上升连续子序列
- JavaEE Spring框架学习笔记一(理论学习之Bean配置项、作用域)
- 监听软键盘的弹出隐藏
- MySQL安装
- [ZJOI2001] 积木城堡
- C\C++ struct && class 综述
- LintCode : 最长连续序列
- COM笔记(2)
- 绯浅的2016年知识管理计划
- (OK) Android系统启动-SystemServer上篇
- javaweb查询所有登录的用户,判断是否在线
- java android utf16转Unicode
- Centos7 搭建nginx
- vmware中已有的Ubuntu虚拟机如何扩展硬盘容量
- 字符串子串替换