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.

题目大意:给一个无序数组,找出最大的连续元素的长度。

说真的,一开始这题,我想半天只想到用boolean数组mark,后来看了别人的才发现可以用set做。

解题思路:

把所有的数组放到set中,然后遍历数组的元素,然后分别向前、向后寻找set中是否存在,记录max。

    public int longestConsecutive(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
Set<Integer> set = new HashSet<>();
for(int num:nums){
set.add(num);
}
int max = 0;
for(int i=0;i<nums.length;i++){
Integer num = nums[i];
int cnt = 1;
while(set.contains(--num)){
cnt++;
set.remove(num);
}
num=nums[i];
while(set.contains(++num)){
cnt++;
set.remove(num);
}
max=Math.max(max,cnt);
}
return max;
}

Longest Consecutive Sequence——Leetcode的更多相关文章

  1. 128. Longest Consecutive Sequence(leetcode)

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

  2. Binary Tree Longest Consecutive Sequence -- LeetCode

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  3. Longest Consecutive Sequence [LeetCode]

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

  4. Longest Consecutive Sequence leetcode java

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

  5. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  7. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  8. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

  9. [LeetCode] 128. Longest Consecutive Sequence 解题思路

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

随机推荐

  1. webrtc学习———记录一

    最近导师让研究一下webrtc,希望将来用到我们的ICT2系统中. 但是从来没有过做web的基础,无论前端还是后端,html.js全都从头学起.html还好说,没有太过复杂的东西. js就有点难度了, ...

  2. 解决weblogic与系统时间相差8小时的问题

    解决weblogic与系统时间相差8小时的问题 在一般情况下weblogic与系统时间是很少会出现时间差的问题,但有可能在某一特定的情况下就会出现,如使用weblogic8版本时可能会出现时差问题: ...

  3. 如何把visual studio 2010的工程文件迁入TFS2010中管理

    如何在VS2010里面创建项目并添加到TFS2010里面. 新建一个项目,并把它添加到TFS,我们会收到下面的错误: 这是因为我们没有为项目创建Team project,而把它直接添加到了Team p ...

  4. css字体大小设置em与rem的区别

    em 单位em是相对于父元素的,如果父元素没有设置字体大小,那就会追溯到body. 比如  如果我在box_text的父元素box加了一个字体大小   那么body的8px就会被box_text的父元 ...

  5. JS操作SELECT方法

    1.判断select选项中 是否存在Value="paraValue"的Item2.向select选项中 加入一个Item3.从select选项中 删除一个Item4.修改sele ...

  6. Android 中 View移动总结:ViewDragHelper学习及用法详解

    如上图简单呈现出两个方块后,提出一个需求: 1.拖动方块时,方块(即子View)可以跟随手指移动. 2.一个方块移动时,另一个方块可以跟随移动. 3.将方块移动到左边区域(右边区域)后放开(即手指离开 ...

  7. Ubuntu 下 安装QQ 截图工具

    1.由于ubuntu下是没有dll动态链接库的,所以需要安装一个软件wine,有这个东西之后,以后在ubuntu下就可以运行exe文件了.(wine是一款优秀的Linux系统平台下的模拟器软件,用来将 ...

  8. odoo8 email

    ?? return 地址和 from 地址不一致,为什么不能发送成功? replay地址. 根据对象(比如purchase.order)发送邮件,可以通过supermessage_get_email_ ...

  9. 在Swift中使用libxml2

    // // main.swift // C150805_libxml2r2 // http://git.oschina.net/yao_yu/Swift2015/tree/master/C150805 ...

  10. 2016022607 - redis配置文件

    在Redis有配置文件(redis.conf)可在Redis的根目录下找到.可以通过Redis的CONFIG命令设置所有Redis的配置. Redis的CONFIG命令的基本语法如下所示: redis ...