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.

===================

给一个未排序数组,找到一个最长的连续数组,返回其长度.

====

思路:

利用一个hash表,存储已经扫描的数组元素,

对数组中每一个curr元素,都必须在hash表中向前查找,向后查找,找出此nums[curr]过在连续数组。

maxlength = max(maxlength,length);

===

code:

class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_map<int,bool> hash_used;
for(auto i:nums){
hash_used[i] = false;
}
int longest = ;
for(auto i:nums){
if(hash_used[i])
continue;
int length = ; hash_used[i] = true; for(int j = i+;hash_used.find(j)!=hash_used.end();j++){
hash_used[j] = true;
length++;
}
for(int j = i-;hash_used.find(j)!=hash_used.end();j--){
hash_used[j] = true;
length++;
}
longest = max(longest,length);
}//for
return longest;
}
};

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

  1. 128. Longest Consecutive Sequence(leetcode)

    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. 【LeetCode】128. Longest Consecutive Sequence

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

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

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

  5. 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度

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

  6. leetcode 128. Longest Consecutive Sequence ----- java

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

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

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

  8. 128. Longest Consecutive Sequence最长连续序列

    [抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  9. 128. Longest Consecutive Sequence (HashTable)

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

随机推荐

  1. web标准的可用性和可访问性

    在Web前端开发界,有三个词经常被提及:可用性(Usability).可访问性(Accessibility)和可维护性(Maintainability). 可用性指的是:产品是否容易上手,用户能否完成 ...

  2. activiti 中的签收与委托 操作

    原文:http://my.oschina.net/acitiviti/blog/350957 先看看activiti中关于TASK的数据库表: 其中有两个字段:OWNER_,ASSIGNEE_ 这两个 ...

  3. Android仿计算器界面

    代码如下: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...

  4. UVA-12436 Rip Van Winkle's Code (线段树区间更新)

    题目大意:一个数组,四种操作: long long data[250001]; void A( int st, int nd ) { for( int i = st; i <= nd; i++ ...

  5. PPT图片快速编辑技巧

    修改*.ppt或*.pptx的后缀名为zip 例如:demo.pptx修改为demo.zip 修改为, 使用压缩软件打开此压缩包, 一般图片资源都会存放在ppt/media下 找到你所要修改的图片,然 ...

  6. java的nio之:java的nio系列教程之概述

    一:java的nio的核心组件?Java NIO 由以下几个核心部分组成: ==>Channels ==>Buffers ==>Selectors 虽然Java NIO 中除此之外还 ...

  7. 解决Ubuntu下Sublime Text 3无法输入中文

    前言 sublime很好用,但是ubuntu下不能输入中文,这是一个很大的问题.不知道为什么开发着一直也不解决,好在还是有高手在,总能找到方法.网上方法很多,但是也很乱,现在我将自己的经验总结一下. ...

  8. 关于$.ajax中data字段的整理--包括json转换和spring注解

    1.前端$.ajax 的data为json提交的时候,后台方法中必须使用@RequestBody 注解    @RequestMapping(value = "getCpuData/{int ...

  9. JavaScript 的字符串转换

    数字.布尔值等其他数据类型都可以转换成字符串:一般来说,脚本引擎将根据上下文自动完成这样的转换.例如,当把数字或布尔型变量传给希望接收字符串变量的函数时,就会先隐式地将该数值转换成字符串,再进行处理: ...

  10. OpenJudge计算概论-中位数

    /*============================================================== 中位数 总时间限制: 2000ms 内存限制: 65536kB 描述 ...