题目

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 )"

本意是跳过已经访问过的元素。。。但是这么写如果遇到了访问过的元素,就不是跳过了,而是直接退出循环了。这是个思维的陷阱,记下来,下次不要再犯。

最新文章

  1. ubuntu下面更改用户名的方法
  2. javascript时间戳和日期字符串相互转换
  3. android 百度地图 通过剪裁图片添加 Marker
  4. Python学习二---字符串
  5. iOS数据本地持久化
  6. 使用Visual Leak Detector检测内存泄漏[转]
  7. VMware系统运维(五)安装SSO vCenter Single Sign-On
  8. 青瓷qici - H5小游戏 抽奖机 0 创建工程
  9. Ubuntu Server忘记密码后,单用户模式修改密码进去不了桌面的无奈
  10. iOS:(接口适配器3)--iPhone适应不同型号 6/6plus 前
  11. compare.go
  12. Inheritance: &#39;A&#39; is an inaccessible base of &#39;B&#39;
  13. 安装vs2017后造成无法打开xproj项目无法打开
  14. django的url反向解析
  15. MySQL 5.6 以上版本支持三种sql_mode模式:ANSI、TRADITIONAL和STRICT_TRANS_TABLES。
  16. python 3.6练习题(仿购物车)
  17. ERROR 1067 (42000): Invalid default value for &#39;created_time&#39;【转】
  18. TED #10# A rite of passage for late life
  19. python3 django连接mysql,同步表结构
  20. POSIX systemV共享内存的区别

热门文章

  1. Tomcat中配置JAVA_HOME
  2. C#问题记录-CallbackOnCollectedDelegate
  3. 关于dependencies和devDependencies的理解
  4. 基于mllib的协同过滤实战(电影推荐)
  5. int _tmain(int argc, _TCHAR* argv[])
  6. Missing map from Nullable`1 to String. Create using Mapper.CreateMap&lt;Nullable`1, String&gt;. 解决办法
  7. 【BZOJ1216】[HNOI2003] 操作系统(堆+模拟)
  8. Shell重启Tomcat脚本
  9. Java控制语句例题,for循环语句,if条件语句等,Scanner类与Random类,Math.max()方法
  10. Linux命令安装vnc服务端与vnc的客户端