import java.util.HashSet;
import java.util.Set; /**
* Source : https://oj.leetcode.com/problems/longest-consecutive-sequence/
*
*
* Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
*
* For example,
* Given [100, 4, 200, 1, 3, 2],
* The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
*
* Your algorithm should run in O(n) complexity.
*/
public class LongestConsecutiveSequence { /**
* 求出数组中元素能构成连续序列的最大长度
*
* 题目中要求是O(n),所以不能使用排序的办法
* 针对每个元素arr[i]寻找数组中向前、向后有没有能构成连续序列的元素,查找的时候利用hash表
* 每次记录最大长度,如果找到了就把该元素从hash表中删除,因为该元素不会被再次查找到,所以从hash表中移除
* 如果hash表为空了,说明所有元素都被查找过,退出循环
*
* @param arr
* @return
*/
public int findLongestSequence (int[] arr) {
int max = 0;
Set<Integer> set = new HashSet<Integer>(arr.length);
for (int i = 0; i < arr.length; i++) {
set.add(arr[i]);
} for (int i = 0; i < arr.length; i++) {
int curnum = arr[i] - 1;
int len = 1; // 查找curnum左边的数字
while (set.contains(curnum)) {
set.remove(curnum);
curnum -= 1;
len++;
} curnum = arr[i] + 1;
while (set.contains(curnum)) {
set.remove(curnum);
curnum += 1;
len++;
} max = Math.max(max, len);
if (set.size() <= 0) {
return max;
}
}
return max;
} public static void main(String[] args) {
LongestConsecutiveSequence longestConsecutiveSequence = new LongestConsecutiveSequence();
System.out.println(longestConsecutiveSequence.findLongestSequence(new int[]{100, 4, 200, 1, 3, 2}) + "----4");
}
}

leetcode — longest-consecutive-sequence的更多相关文章

  1. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

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

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

  3. LeetCode: Longest Consecutive Sequence 解题报告

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

  4. [leetcode]Longest Consecutive Sequence @ Python

    原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...

  5. [LeetCode] Longest Consecutive Sequence

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

  6. LeetCode: Longest Consecutive Sequence [128]

    [题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

  7. Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明

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

  8. LeetCode—Longest Consecutive Sequence

    题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  9. [Leetcode] Longest consecutive sequence 最长连续序列

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

  10. [Leetcode] Longest Consecutive Sequence 略详细 (Java)

    题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...

随机推荐

  1. C# 神奇的Web services 请求超时问题 排查分析

    服务器上有两个接口,一个是Web Services(asmx文件)接口,一个是MVC API (普通的GET请求接口) 神奇的事情是这样的,只要我使用WebRequest请求两次,再使用Web Ser ...

  2. 新版CSDN-markdown编辑器使用指南

    本文来自CSDN官方,分markdown原文和实际显示部分,推荐开两个窗口对比浏览 Markdown部分 @[TOC](这里写自定义目录dd标题) # 欢迎使用Markdown编辑器 你好! 这是你第 ...

  3. LDAP & Implementation

    LDAP & Implementation 一.什么是LDAP? (一)在介绍什么是LDAP之前,我们先来复习一个东西:“什么是目录服务?” 1. 目录服务是一个特殊的数据库,用来保存描述性的 ...

  4. mysql命名规范

    1.数据库表命名规范: (1)表名前应该加上前缀,表的前缀一个用系统或模块的英文名称缩写,前缀全部大写或首字母大写,表名中包含的单词首字母大写. (2)数据库表名应该有意义,并且易于理解,最好使用可以 ...

  5. Matlab调用遗传工具箱复现论文模型求解部分

    原文转载自:https://blog.csdn.net/robert_chen1988/article/details/52431594 论文来源: https://www.sciencedirect ...

  6. linux 做了raid后,硬盘坏了更换问题

    系统做完raid1后发现 raid盘坏了,硬盘都是热插拔的,更换后,需要简单配置一下才能自动进行镜像拷贝. 在pd mgmt 页面,选择新加入的硬盘,按F2,选择 make global HS选项 选 ...

  7. 使用POST下载文件

    一直以来,JS都没有比较好的可以直接处理二进制的方法.而Blob的存在,允许我们可以通过JS直接操作二进制数据.一.下载util.fetchDownload= function (opt,data) ...

  8. JS区分对象类型

    Object.prototype.toString.call() 区分对象类型 在JavaScript中数据类型分为:1.基本类型,2.引用类型 基本类型:Undefined,Boolean,Stri ...

  9. Linux shell 编程文件比较

    文件比较允许你测试Linux 文件系统上文件和目录的状态 比较 描述 -d file  检查file是否存在并是一个目录 -e file 检查file是否存在 -f file 检查file是否存在并是 ...

  10. Oracle 闪回 找回数据

    使用闪回技术,实现基于磁盘上闪回恢复区的自动备份与还原. 一.恢复表对象 1.创建学生表 create table STUDENT ( idno INTEGER, name VARCHAR2(30), ...