博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Longest Consecutive Sequence
阅读量:7109 次
发布时间:2019-06-28

本文共 616 字,大约阅读时间需要 2 分钟。

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 table查找的时间复杂度O(1),可以是时间复杂度降为O(n)。

1 class Solution { 2 public: 3     int longestConsecutive(vector
& nums) { 4 if(nums.size()<1) return 0; 5 int result=0; 6 unordered_map
showed; 7 for(int i=0;i

 

转载于:https://www.cnblogs.com/Sean-le/p/4815029.html

你可能感兴趣的文章
iOS开发网络篇—发送GET和POST请求(使用NSURLSession)
查看>>
Adaptability Is Accessibility
查看>>
HDU_1227_Fast Food_动态规划
查看>>
实验验证redis的快照和AOF
查看>>
临时表的应用
查看>>
码农的福利来了, 编程在线Androd 客户端上线了
查看>>
sys.stdout.write与sys.sterr.write(二)
查看>>
多继承时,多个基类中存在型别相同的虚函数,该怎么做?
查看>>
shell配置,选择,环境变量修改(ORACLE_HOME,ORACLE_SID),无法使用sqlplus
查看>>
Design Hint for Inheritance(继承设计的一些小贴士)
查看>>
java 时间格式化函数
查看>>
python参数
查看>>
P1614 爱与愁的心痛
查看>>
Windows上使用Objective-c和Cocoa
查看>>
android ui事件处理分析
查看>>
我的爹娘(二)
查看>>
ctrl+c关闭多线程python程序
查看>>
Algorithm4.子数组求和贪心
查看>>
Count Color
查看>>
349B - C. Mafia
查看>>