1. import java.util.HashSet;
  2. import java.util.Set;
  3. /**
  4. * Source : https://oj.leetcode.com/problems/longest-consecutive-sequence/
  5. *
  6. *
  7. * Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
  8. *
  9. * For example,
  10. * Given [100, 4, 200, 1, 3, 2],
  11. * The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
  12. *
  13. * Your algorithm should run in O(n) complexity.
  14. */
  15. public class LongestConsecutiveSequence {
  16. /**
  17. * 求出数组中元素能构成连续序列的最大长度
  18. *
  19. * 题目中要求是O(n),所以不能使用排序的办法
  20. * 针对每个元素arr[i]寻找数组中向前、向后有没有能构成连续序列的元素,查找的时候利用hash表
  21. * 每次记录最大长度,如果找到了就把该元素从hash表中删除,因为该元素不会被再次查找到,所以从hash表中移除
  22. * 如果hash表为空了,说明所有元素都被查找过,退出循环
  23. *
  24. * @param arr
  25. * @return
  26. */
  27. public int findLongestSequence (int[] arr) {
  28. int max = 0;
  29. Set<Integer> set = new HashSet<Integer>(arr.length);
  30. for (int i = 0; i < arr.length; i++) {
  31. set.add(arr[i]);
  32. }
  33. for (int i = 0; i < arr.length; i++) {
  34. int curnum = arr[i] - 1;
  35. int len = 1;
  36. // 查找curnum左边的数字
  37. while (set.contains(curnum)) {
  38. set.remove(curnum);
  39. curnum -= 1;
  40. len++;
  41. }
  42. curnum = arr[i] + 1;
  43. while (set.contains(curnum)) {
  44. set.remove(curnum);
  45. curnum += 1;
  46. len++;
  47. }
  48. max = Math.max(max, len);
  49. if (set.size() <= 0) {
  50. return max;
  51. }
  52. }
  53. return max;
  54. }
  55. public static void main(String[] args) {
  56. LongestConsecutiveSequence longestConsecutiveSequence = new LongestConsecutiveSequence();
  57. System.out.println(longestConsecutiveSequence.findLongestSequence(new int[]{100, 4, 200, 1, 3, 2}) + "----4");
  58. }
  59. }

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. 面试题int和Integer

    int和Integer的区别 1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的引 ...

  2. :nth-child() 与 :nth-of-type(n)的区别

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 各个模块的刷新js

    // 更新页面中的subgrid function refreshSubGrid(subgridName) { Xrm.Page.ui.controls.get(subgridName).refres ...

  4. MySQL5.6启用sha256_password插件

    一.背景: 使用MySQL5.6过程中,发现默认的加密插件为mysql_native_password.而sha256_password的安全程度要比mysql_native_password高,尝试 ...

  5. 2.Git配置和关联GitHub

    1.配置本地信息, 右键Git Bush Here git config –global user.name '账号名' ##回车 git config –global user.email 邮箱 # ...

  6. 4.23 Linux(3)

    2019-4-23 19:03:53 买的服务器第三天感觉超爽!! 发现学习Linux超爽,有种操作的快感!!!!!是Windows比不了的!! 阿里巴巴镜像源 : https://opsx.alib ...

  7. 20175324 《Java程序设计》第七周学习总结

    教材学习内容总结 常用实用类 String类 - 程序可以直接使用String类,但不能进行扩展,即String类不可以有子类 - 常用构造方法 - String(char a[])用一个字符数组a创 ...

  8. Java课程之团队开发(团队介绍)

    一.介绍团队和团队成员 团队名称:凯域软创 团队成员介绍:张某某,崔某某,焦某某,陈某 二.关于团队作品 1.作品名称:课程表 2.你的创意解决了用户的什么需求:查看课程信息的需求 3.你有什么招数用 ...

  9. Linux Rabbit的使用

    安装RabbitMQ 1.安装Erlang yum -y install epel-release yum -y update yum -y install erlang socat yum -y i ...

  10. FFmpeg 结构体学习(六): AVCodecContext 分析

    在上文FFmpeg 结构体学习(五): AVCodec 分析我们学习了AVCodec结构体的相关内容.本文,我们将讲述一下AVCodecContext. AVCodecContext是包含变量较多的结 ...