128. Longest Consecutive Sequence
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的更多相关文章
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 128. Longest Consecutive Sequence最长连续序列
[抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- 128. Longest Consecutive Sequence (HashTable)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- 使用Jsoup函数包抓取网页内容
之前写过一篇用Java抓取网页内容的文章,当时是用url.openStream()函数创建一个流,然后用BufferedReader把这个inputstream读取进来.抓取的结果是一整个字符串.如果 ...
- 移动端动画使用transform提升性能
在移动端做动画,对性能要求较高而通常的改变margin属性是性能极低的,即使使用绝对定位改变top,left这些属性性能也很差因此应该使用transform来进行动画效果,如transform:tra ...
- 【转】非常适用的Sourceinsight插件,提高效率事半功倍
原文网址:http://www.cnblogs.com/wangqiguo/p/3713211.html 一直使用sourceinsight编辑C/C++代码,sourceinsight是一个非常好用 ...
- 时事新闻之 谷歌 google 发布Tensor Flow 源代码
TensorFlow: TensorFlow is an open source software library for numerical computation using data flow ...
- MUA
a big deal analysis analytics cooperate 合作 efficient explicitly fine grained Granularity graph geogr ...
- JSBinding + SharpKit / 初体验:下载代码及运行Demo
QQ群:189738580 以下是群主维护的JSB版本: git地址:https://github.com/qcwgithub/qjsbunitynew.git插件源码地址(不包含SpiderMonk ...
- CUDA中的流与事件
流:CUDA流很像CPU的线程,一个CUDA流中的操作按顺序进行,粗粒度管理多个处理单元的并发执行. 通俗的讲,流用于并行运算,比如处理同一副图,你用一个流处理左边半张图片,再用第二个流处理右边半张图 ...
- Linux-SSL和SSH和OpenSSH,OpenSSL有什么区别
ssl是通讯链路的附加层.可以包含很多协议.https, ftps, ..... ssh只是加密的shell,最初是用来替代telnet的.通过port forward,也可以让其他协议通过ssh的隧 ...
- python datetime
不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部的方法,详解python操作日期和时间的方法.1.将字符串的时间转换为时间戳 ...
- unity, ugui input field
ugui Input Field,获取输入的字符串. 错误方法: string content=inputField.FindChild("Text").text; 这样得到的是输 ...