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. css 导航条 布局

    导航条(简单易用的导航) <!DOCTYPE html> <html> <head> <style> ul { list-style-type:none ...

  2. 《你不常用的c#之四》:Array的小抽屉ArraySegment

    转载自csdn:http://blog.csdn.net/robingaoxb/article/details/6200060 一:)略谈      ArraySegment顾名思义就是Array区块 ...

  3. 利用反射把查询到的Table、Reader转换成List、Model

    菜鸟一枚,入园已有两年三个月,这还是第一次写博客,请各位大神斧正. 这是我写的一个工具类,通常我们从数据库查询到一个  DataReader  或者是  一个 Table , 想要转换成 一个 lis ...

  4. The Better Way to Debug Your JavaScript Program

    Debugging JS program is not as easy as we do in Visual Studio or any other IDE. I usually us "a ...

  5. JS控制文字一个一个出现

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

  6. obj.onclick=fnClick与obj.onclick=fnClick()的区别

    先说结论:这段代码浏览器会报错,提示 aDiv[this.index] is undefined 所以正确的写法应该是去掉(),直接写为function fnClick.   不加括号的话,相当于指定 ...

  7. (转)JS页面间传值

    一:JavaScript静态页面值传递之URL篇 能过URL进行传值.把要传递的信息接在URL上. 例子: 参数传出页面Post.htm—> <input type="text& ...

  8. 批量缩放PNG图片.

    最近需要缩放N多图片, 找遍了互联网也没有找到方便使用的批量缩放工具.. 趁着周末写一个练手.. #include <iostream> #include <vector> # ...

  9. 插入标记 方法 insertAdjacentHTML

    html5新增的插入标记方法,insertAdjacentHTML() 可以接受2个参数 插入位置和要插入的 HTML 文本.第一个参数必须是下列值之一: "beforebegin" ...

  10. yii2源码学习笔记(十五)

    这几天有点忙今天好些了,继续上次的module来吧 /** * Returns the directory that contains the controller classes according ...