【Longest Consecutive Sequence】cpp
题目:
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.
代码:
class Solution {
public:
int longestConsecutive(vector<int> &num) {
// hashmap record if element in num is visited
std::map<int,bool> visited;
for(std::vector<int>::iterator i = num.begin(); i != num.end(); ++i)
{
visited[*i] = false;
}
// search the longest consecutive
unsigned int longest_global = ;
for(std::vector<int>::iterator i = num.begin(); i != num.end(); ++i)
{
if(visited[*i]) continue;
unsigned int longest_local = ;
for(int j = *i+; visited.find(j) != visited.end(); ++j)
{
visited[j] = true;
++longest_local;
}
for(int j = *i-; visited.find(j) != visited.end(); --j)
{
visited[j] = true;
++longest_local;
}
longest_global = std::max(longest_global, longest_local+);
}
return longest_global;
}
};
Tips:
1. 要想O(n), 而且无序,只能结合hashmap
2. 这里需要明确的一个逻辑是,通过hashmap前后访问,可以把包含当前元素的最大连同序列都找出来;而且访问过的元素不用再访问。
=======================================================
第二次过这道题,大体的思路能顺下来,但是疏忽了几个细节。AC代码如下:
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int global_longest = 1;
// initialize map
unordered_map<int, bool> visited;
for ( int i=0; i<nums.size(); ++i ) visited[nums[i]]=false;
// go one traversal
for ( int i=0; i<nums.size(); ++i )
{
if ( visited[nums[i]] ) continue;
visited[nums[i]] = true;
int local_longest = 1;
int curr = nums[i]-1;
// search towards smaller direction
while ( visited.find(curr)!=visited.end() )
{
local_longest++;
visited[curr] = true;
curr--;
}
// search towards larger direction
curr = nums[i]+1;
while ( visited.find(curr)!=visited.end() )
{
local_longest++;
visited[curr] = true;
curr++;
}
// update global longest
global_longest = std::max(global_longest, local_longest);
}
return global_longest;
}
};
tips:
1. 根据某个元素往‘前’、‘后’两个方向遍历之前,要先记得把该元素设置为访问过(即,visited[nums[i]] = true;)
2. 第一遍把while循环中的 visited[curr]=true都写成了visited[nums[i]],这个纯属低级错误,不要再犯
3. 第一遍有一个逻辑上的错误:
"for ( int i=0; i<nums.size() && !visited[nums[i]]; ++i )"
本意是跳过已经访问过的元素。。。但是这么写如果遇到了访问过的元素,就不是跳过了,而是直接退出循环了。这是个思维的陷阱,记下来,下次不要再犯。
【Longest Consecutive Sequence】cpp的更多相关文章
- 【Longest Common Prefix】cpp
题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...
- 【Longest Palindromic Substring】cpp
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
随机推荐
- C#对话框-打开和保存对话框(转)
//打开文件 OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.In ...
- java 千分位的添加和去除
转至:http://blog.sina.com.cn/s/blog_8f99a1640102v1xh.html 将一个数字转换为有千分位的格式: NumberFormat numberFormat1 ...
- ffmpeg 资源[转]
http://blog.csdn.net/leixiaohua1020/article/details/15811977 一. FFmpeg主站 1. FFmpeg的源码发布,资料 网址: htt ...
- m3u8视频下载方法
部分网站的视频内容,采用了m3u8的格式.正常打开网页可以,但是如果想下载到本地,就存在一定问题了.这里可以再获取到m3u8地址之后,利用vlc软件,来下载m3u8的视频. 工具:Firefox浏览器 ...
- Flutter /bin/sh: /packages/flutter_tools/bin/xcode_backend.sh: No such file or directory
自己写项目中遇到的一个问题, 可以出来是路径找不到,应该是FLUTTER_ROOT这个全局变量没有取到值的原因 1.检查xcode_backend.sh 是否真的存在 2.网上说的:Target -& ...
- LuceneTest
/** * Created by mhm on 2019/6/24. */@RunWith(SpringJUnit4ClassRunner.class)public class LuceneTest ...
- form 表单 和 jQuery HTML / CSS 方法($().html 类似的样式)
1 有关链接 :http://www.runoob.com/tags/tag-form.html https://www.cnblogs.com/Jxwz/p/4509618.html https:/ ...
- Andrew NG 自动化所演讲(20140707):DeepLearning Overview and Trends
出处 以下内容转载于 网友 Fiona Duan,感谢作者分享 (原作的图片显示有问题,所以我从别处找了一些附上,小伙伴们可以看看).最近越来越觉得人工智能,深度学习是一个很好的发展方向,应该也是未来 ...
- C#自减运算符
一.C#自减运算符(--) 自减运算符(--)是将操作数减1. 1. 前缀自减运算符 前缀自减运算符是“先减1,后使用”.它的运算结果是操作数减1之后的值. 例如: --x; // 前缀自减运算符 ...
- C#继承机制 继承与访问修饰符
继承与访问修饰符 访问修饰符是一些关键字,用于指定声明的成员或类型的可访问性.类的继承中有四个访问修饰符: public protected internal private.使用这些访问修饰符可指定 ...