Question :
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.
Anwser 1 :
class Solution {
public:
int longestConsecutive(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
map<int, int> hmap;
hmap.clear();
int n = num.size();
for(int i=0; i<n; i++){
//hmap.insert(pair<int, int>(num[i], i));
hmap[num[i]] = i;
}
int ans=0, cnt=0;
map<int, int>::iterator it;
for(int i=0; i<n; i++)
{
int cur = num[i];
it = hmap.find(num[i]); // value
cnt++;
if(it!=hmap.end())
{
map<int, int>::iterator iter;
while(1){
iter = hmap.find(++cur); // to larger value
if(iter == hmap.end())
break;
cnt++;
hmap.erase(iter);
}
cur=num[i];
while(1){
iter = hmap.find(--cur); // to smaller value
if(iter == hmap.end())
break;
cnt++;
hmap.erase(iter);
}
ans = cnt > ans ? cnt : ans;
}
cnt=0; // init to count remaider value of hmap
}
return ans;
}
};
注意点:
1) 采用map,value为map_key, index为map_value
2) 按value++, value--查找,找到了则从map移除(erase),减少for循环find的次数
3) for + while,时间复杂度最坏其实为O(n*n),但是仍然编译通过了...
Anwser 2 :
class Solution {
public:
int getCount(map<int, int> &hmap, int value, bool asc){
int count = 0;
while(hmap.find(value) != hmap.end()){
hmap.erase(value);
count++;
if(asc){
value++;
} else {
value--;
}
}
return count;
}
int longestConsecutive(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
map<int, int> hmap;
hmap.clear();
int n = num.size();
for(int i=0; i<n; i++){
//hmap.insert(pair<int, int>(num[i], i));
hmap[num[i]] = i;
}
int ans=0, cnt=0;
for(int i=0; i<n; i++)
{
int count = getCount(hmap, num[i], false) + getCount(hmap, num[i]+1, true);
ans = count > ans ? count : ans;
}
return ans;
}
};
注意点:
1) 改进了方法1
2) getCount()找到则移除,count++,继续寻找下一个
3) count = getCount(hmap, num[i], false) + getCount(hmap, num[i]+1, true); 注意暗红色部分,加1为因为之前num[i]已经被移除了,因此找其上面一个value + 1
Anwser 3 :
class Solution {
public:
int longestConsecutive(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
map<int, int> hmap;
hmap.clear();
int n = num.size();
for(int i=0; i<n; i++){
hmap.insert(pair<int, int>(num[i], 1)); // map will auto sort by key(num[i])
//hmap[num[i]] = 1;
}
int ans = 1; // min init
int pre_k = 0;
int pre_v = 0;
map<int, int>::iterator it;
for(it = hmap.begin(); it != hmap.end(); it++)
{
if(it == hmap.begin()) {
pre_k = it->first;
pre_v = it->second;
continue;
}
if(it->first == pre_k + 1){
it->second = pre_v + 1;
ans = it->second > ans ? it->second : ans;
}
pre_k = it->first;
pre_v = it->second;
}
return ans;
}
};
注意点:
1) 方法2的进一步改进
2) 此方法最大的利用了map自动根据key(num[i])排序,且value设为了1(起始值)
分享到:
相关推荐
python python_leetcode题解之128_Longest_Consecutive_Sequence
javascript js_leetcode题解之128-longest-consecutive-sequence.js
* Longest Consecutive Sequence Java:该题目要求找到最长的连续序列,实现方法使用了哈希表和迭代算法。 * Search a 2D Matrix:该题目要求在二维矩阵中查找元素,实现方法使用了二分查找算法。 * Rotate Image:...
Binary Tree Longest Consecutive Sequence等。这类题目通常要求读者使用树的各种操作来解决问题,例如树的遍历、查找、插入等操作。 4. 图类题目 图类题目是LeetCode原题中的一个复杂类题目,本文中涵盖了多个图...
Consecutive Sequence 7 Two Sum Hash,夹逼均可 8 3Sum Hash法转换2sum 9 3Sum Closest Sort +夹逼法 10 4Sum Sort +夹逼法 11 Remove Element 12 Next Permutation 公式 13 Permutation Sequence 公式 14 Valid ...
2. **2.1.6 Longest Consecutive Sequence**:给定一个未排序的整数数组,找出其中最长连续序列的长度。 3. **2.1.7 Two Sum**:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个...
leetcode 分类 Leetcode代码题解 随缘刷题,选择性更新题解 LeetCode 94 144 145 ...longest-consecutive-sequence 最长连续序列: Leetcode 155 Min Stack 最小栈: LeetCode 773 滑动谜题: ......
# [LeetCode](https://leetcode.com/problemset/algorithms/)  [![License]...
LeetCode的第128题,名为"Longest Consecutive Sequence",要求找到给定无序整数数组中,最长的连续整数序列的长度。例如,对于输入数组 [100, 4, 200, 1, 3, 2],最长连续序列是 [1, 2, 3, 4],其长度为4。 首先,...
leetcode正方体收藏TakeUforward-SDE_180 要了解整个列表和其他内容,如项目、简历、如何进行面试……观看整个视频: 在以下位置找到展示位置系列: ...Consecutive Sequence Longest Subarray with 0 sum 给定
longest-consecutive-sequence max-area-of-island next-greater-element-ii serialize-and-deserialize-binary-tree subarray-sum-equals-k binary-tree-preorder-traversal n-queens-ii populating-next-right-...
例如,"最长连续序列"(Longest Consecutive Sequence)题目的解决方案通常会用到哈希表来存储数组元素及其出现的次数,再结合深度优先搜索或广度优先搜索策略找出最长的连续序列。 3. 优化技巧:在解决LeetCode...
5. **动态规划**:如“最长连续序列”(Longest Consecutive Sequence),要求找到一个无序整数数组中最长的连续子序列。 6. **回溯法**:如“全排列”(Permutations),要求列出所有可能的数组排列。 7. **堆和队列*...
比如,“最长连续序列”(Longest Consecutive Sequence)要求找到给定无序整数数组中最长的连续序列长度,这涉及到哈希表和递归/迭代的巧妙结合。 LeetCode平台的开源特性意味着用户可以查看其他人的解决方案,...
leetcode SDE-问题 标准 SDE 问题列表 第一天:(数组) 日 问题陈述 解决方案 困难 使用的数据结构 使用的算法 时间复杂度 空间复杂度 补充阅读 在 N 个整数的数组中查找重复项 中等的 大批 不适用 上) O(1) 在不...
- **2.1.6 Longest Consecutive Sequence** - 找出数组中最长的连续子序列。 - 实现思路:可以使用哈希表存储每个元素及其连续状态,然后遍历数组找到最长的连续序列。 - **2.1.7 Two Sum** - 寻找数组中两个数...
4. **滑动窗口**:在数组中使用滑动窗口进行统计,如“最长连续序列”(Longest Consecutive Sequence)。 5. **数组分割**:根据特定条件将数组分割成多个子数组,如“分割数组的最大和”(MaxChunks ToSorted)。...
5. **滑动窗口最大值/最小值**:例如“数组中的最长连续序列”(Longest Consecutive Sequence),哈希表可以帮助我们快速查询元素及其相邻元素的状态,从而找到最长连续序列。 6. **统计问题**:如“数组中出现...
此外,"Leetcode-main"可能还包含了其他挑战,如“最长连续序列”(Longest Consecutive Sequence)来演示如何使用集合进行有效的状态追踪,或者“最大子数组和”(Maximum Subarray)来展示动态规划的应用。每个问题的...