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.

就是判断数组中最长的连续数字的长度。

1、直接用排序。。当然不行了,时间复杂度超过了O(n),虽然AC了。

public class Solution {
public int longestConsecutive(int[] nums) {
int result = 1;
if( nums.length == 0)
return result;
Arrays.sort(nums);
for( int i = 1;i<nums.length;i++){
int a = 1;
while( i<nums.length && (nums[i] == nums[i-1]+1 || nums[i] == nums[i-1]) ){
if( nums[i] == nums[i-1]+1 )
a++;
i++;
}
result = Math.max(a,result); }
return result; }
}

2、用set集合。遍历两次,得出结果,由于add,remove,contains都是O(1)的复杂度,所以时间复杂度符合题意。

public class Solution {
public int longestConsecutive(int[] nums) {
if( nums.length == 0)
return 0;
Set set = new HashSet<Integer>(); for( int num : nums)
set.add(num);
int result = 1; for( int e : nums){
int left = e-1;
int right = e+1;
int count = 1;
while( set.contains(left )){
set.remove(left);
count++;
left--;
}
while( set.contains(right) ){
set.remove(right);
count++;
right++;
}
result = Math.max(result,count); }
return result; }
}

leetcode 128. Longest Consecutive Sequence ----- java的更多相关文章

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

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

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

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

  3. Java for LeetCode 128 Longest Consecutive Sequence

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

  4. [leetcode]128. Longest Consecutive Sequence最长连续序列

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

  5. Leetcode 128. Longest Consecutive Sequence (union find)

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

  6. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

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

  7. Leetcode#128 Longest Consecutive Sequence

    原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...

  8. 128. Longest Consecutive Sequence(leetcode)

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

  9. 【LeetCode】128. Longest Consecutive Sequence

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

随机推荐

  1. Session初识

    web服务器没有短期记忆,所以需要使用session来跟踪用户的整个会话活动.会话管理有3种解决方案: 1)使用隐藏域(很少使用) 在显示页面中使用隐藏域来保存会话ID.例如,在JSP中将input标 ...

  2. io函数

    io函数一般分为两大类: 系统(不带缓存)调用: 如read.write.open 标准(带缓存)调用: fread.fwrite.fopen 上面说的带缓存/不带缓存是针对用户态的,内核态本身都是带 ...

  3. Dom事件初步了解

    1.事件流 事件流可以分为两种:事件冒泡和事件捕获 1. 事件冒泡就是从目标元素一直冒泡到根元素html(IE和DOM浏览器都有) 2. 事件捕获就是从根元素到目标元素(DOM浏览器支持) 2.事件处 ...

  4. JS事件大全

    橙色表示“非常常用”  绿色表示“常用” onClick IE3|N2|O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDblClick IE4|N4|O 鼠标双击事件 onMouseD ...

  5. PHP面向对象的继承

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

  6. mycat启动后,用Navicat Premium 连接报 "2013"

    最近在学习mycat,启动后,用Navicat Premium 连接报 "2013"  Lost Connection During Query ,经过一顿百度也没发现是怎么回事, ...

  7. 17、SQL基础整理(事务)

    事务 事务==流程控制 确保流程只能成功或者失败,若出现错误会自动回到原点 例: begin tran insert into student values('111','王五','男','1999- ...

  8. js动态设置窗体位置

    1设置登录框的js,动态设置高度等 <script> $(document).ready(function () { $() / + "px"); $("in ...

  9. session 和 cookie 的区别和联系

    二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当下次你再光临同一个网站,WEB 服务器会先看看有没 ...

  10. Oracle存储过程基本语法

    一.形式 1 CREATE OR REPLACE PROCEDURE 存储过程名  //是一个SQL语句通知Oracle数据库去创建一个叫做skeleton存储过程, 如果存在就覆盖它; 2 IS   ...