leetcode刷题,总结,记录,备忘 128
来源:互联网 发布:linux更改文件权限例子 编辑:程序博客网 时间:2024/05/16 17:01
leetcode128Longest 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.
Subscribe to see which companies asked this question
class Solution {public: int longestConsecutive(vector<int>& nums) { if (nums.size() == 0) { return 0; } if (nums.size() == 1) { return 1; } sort(nums.begin(), nums.end()); nums.erase(unique(nums.begin(), nums.end()), nums.end()); int len = 1; int maxlen = 1; for (vector<int>::iterator it = nums.begin(); it != nums.end() - 1; ++it) { if (*it + 1 == *(it + 1)) { len++; } else { if (len > maxlen) { maxlen = len; } len = 1; } } if (len > maxlen) maxlen = len; return maxlen; }};
0 0
- leetcode刷题,总结,记录,备忘 128
- leetcode刷题。总结,记录,备忘 104
- leetcode刷题。总结,记录,备忘 100
- leetcode刷题。总结,记录,备忘 191
- leetcode刷题。总结,记录,备忘。168
- leetcode刷题, 总结,记录,备忘 171
- leetcode刷题 ,总结,记录,备忘。144
- leetcode刷题,总结,记录, 备忘 141
- leetcode刷题,总结,记录,备忘。116
- leetcode刷题,总结,记录,备忘。96
- leetcode刷题。记录,总结,备忘。217
- leetcode 刷题, 总结,记录,备忘 35
- leetcode刷题,总结,记录,备忘 226
- leetcode刷题,总结,记录,备忘 169
- leetcode刷题,总结,记录,备忘137
- leetcode刷题,总结,记录,备忘83
- leetcode刷题,总结,记录,备忘70
- leetcode刷题,总结,记录,备忘 108
- js简单表格插件开发
- 关于Android优化的小小细节
- 用css3实现鼠标移入在原来的层上面显示另一个层的动画效果
- Swift错误处理(Error Handling)
- 面向对象&面向过程
- leetcode刷题,总结,记录,备忘 128
- HDU 1009
- c陷阱和缺陷——链接、预处理、库函数、可移植性缺陷
- 关于php的register_globals和magic_quotes_gpc参数配置
- source insight实现快捷键打开文件目录
- UML初识
- Unity3D 单例实现方法(方便简单)
- JavaScript设计模式 Item 3 --封装
- 面试总结2--美团测试面试