LeetCode(128) 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.
分析
给定一个整形数组,求出最长的连续序列。例如数组[100,4,200,1,3,2],最长的连续序列长度为[1,2,3,4],长度为4。要求时间复杂度为O(n)。
首先,想到的办法就是 排序—>得到最长连续长度;但是不符合要求的时间复杂度,所以不予采用。
其次,第二个方法是利用c++中的set,直接会排序,并且没有重合的,但是set背后实现的原理牵扯到红黑树,时间复杂度同样不满足。
所以,解决该问题还是应该从哈希入手,建立hash索引,把查找的元素周围的都访问个遍,求出个临时最大值跟全局最大值比较。当再次访问该段的元素的时候,直接跳过。这样保证时间复杂度为O(n)。
c++11中数据结构为unordered_set,保证查找元素的时间复杂度为O(1),但是奇怪的是我才用的unordered_set并没有通过,给出的结果是TLE,于是,我就改用了unordered_map最后通过测试。
AC代码
class Solution {
public:
//方法一:使用unordered_set
int longestConsecutive1(vector<int>& nums) {
if (nums.empty() || nums.size() == 1)
return nums.size();
//保存序列元素个数
int size = nums.size();
unordered_set<int> existNums, visitedNums;
for (int i = 0; i < size; ++i)
{
existNums.insert(nums[i]);
}
int maxCount = 0;
for (int i = 0; i < size; ++i)
{
//每个连续序列中的元素只需要遍历一次,只有不在已遍历序列中时,向两侧探查
if (visitedNums.find(nums[i]) != visitedNums.end())
continue;
int count = 1, less = nums[i] - 1, great = nums[i] + 1;
while (existNums.find(less) != existNums.end())
{
visitedNums.insert(less);
++count;
--less;
}//while
while (existNums.find(great) != existNums.end())
{
visitedNums.insert(great);
++count;
--less;
}//while
if (count > maxCount)
maxCount = count;
}//for
return maxCount;
}
//方法二:使用unordered_map
int longestConsecutive(vector<int>& nums) {
if (nums.empty() || nums.size() == 1)
return nums.size();
//保存序列元素个数
int size = nums.size();
unordered_map<int, bool> visitedNums;
for (int i = 0; i < size; ++i)
{
visitedNums[nums[i]] = false;
}
int maxCount = 0;
for (int i = 0; i < size; ++i)
{
//若已访问过,则跳过
if (visitedNums[nums[i]])
continue;
int count = 1;
//改变访问标记
visitedNums[nums[i]] = true;
int less = nums[i] - 1, great = nums[i] + 1;
while (visitedNums.find(less) != visitedNums.end())
{
visitedNums[less] = true;
--less;
++count;
}//while
while (visitedNums.find(great) != visitedNums.end())
{
visitedNums[great] = true;
++great;
++count;
}//while
if (count > maxCount)
maxCount = count;
}//for
return maxCount;
}
};
LeetCode(128) Longest Consecutive Sequence的更多相关文章
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- LeetCode (32) Longest Valid Parentheses
题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- LeetCode(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode(14)Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...
随机推荐
- python中“生成器”、“迭代器”、“闭包”、“装饰器”的深入理解
python中"生成器"."迭代器"."闭包"."装饰器"的深入理解 一.生成器 1.生成器定义:在python中,一边 ...
- (转)Centos7安装配置NFS服务和挂载
Centos7安装配置NFS服务和挂载 原文:https://www.u22e.com/601.html NFS简介 NFS(Network File System)即网络文件系统,是FreeBSD支 ...
- spirngmvc整合mybatis
一.建立一张简单的User表 CREATE TABLE `users` (`id` int(20) NOT NULL AUTO_INCREMENT,`name` varchar(20) NOT NUL ...
- C# Mutex互斥锁
Mutex 构造函数 (Boolean, String, Boolean) public Mutex ( bool initiallyOwned, string name, out bool crea ...
- vuex填坑记录
vuex是需要等待页面加载完成后才会更新的,如果页面点击刷新有用到vuex的地方,那么vuex会保持旧的数据,等刷新完成后vuex的数据才会重置,所以要在页面加载后再调用vuex的数据才是正确的,如果 ...
- c# Redis操作类
需要添加StackExchange.Redis.dll引用 using System; using System.Collections.Generic; using System.IO; using ...
- android应用开发全程实录-你有多熟悉listview?
今天给大家带来<android应用开发全程实录>中关于listview和adatper中的部分.包括listview的基本使用,listview的优化等. 我们经常会在应用程序中使用列表的 ...
- Android用RecyclerView实现的二维Excel效果组件
excelPanel 二维RecyclerView.不仅可以加载历史数据,而且可以加载未来的数据. 包括在您的项目中 excelPanel 二维RecyclerView.不仅可以加载历史数据,而且 ...
- ftp和sftp
一.ftp ftp是文件传输协议,ftp协议包括两部分,一个是ftp客户端,另一个是ftp服务器. 原理:一般情况下,当使用FTP服务的时候,我们都知道默认是21号端口,其实还有一个20号端口.FTP ...
- node执行cmd命令方法
var cmd='tasklist';//获取 子进程模块的exec方法,用于执行cmd命令var exec = require('child_process').exec; //运行 定义的cmd命 ...