获取一个数组中最长的连续的元素序列
2017-01-05 15:32
393 查看
题目描述:获取一个数组中最长的元素序列。例如,给定了[31,6,32,1,3,2],其中最长的连续的元素序列是[1,2,3],最后返回其长度3
分析:判断当前节点是属于一个序列的,只需判断前一个或者后一个节点也在序列中即可,即判断array[i]-1和array[i]+1也是位于这个序列中即可。如果这样判断的话,就需要保存过程中的部分值,为此,可以采用哈希表来保存计算的中间值。
参考解答:
#include "stdafx.h"
#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;
struct Bound
{
int low;
int high;
Bound(int h=0,int l=0)
{
high=h;
low=l;
}
};
int lengthOfLongestConsecutiveSequence(vector<int> &num)
{
unordered_map<int,Bound> table;
int local;
int maxLen=0;
for(int i=0;i<num.size();i++)
{
if(table.count(num[i])){
continue;
}
local=num[i];
int low=num[i],high=num[i];
if(table.count(local-1)){
low=table[local-1].low;
}
if(table.count(
9f02
local+1))
{
high=table[local+1].high;
}
table[low].high=table[local].high=high;
table[high].low=table[local].low=low;
if(high-low+1>maxLen)
{
maxLen=high-low+1;
}
}
return maxLen;
}
int _tmain(int argc, _TCHAR* argv[])
{
int arr[]={31,6,32,1,2,4};
vector<int> arr2(6);
for(int i=0;i<6;i++)
{
arr2.push_back(arr[i]);
}
int sum=lengthOfLongestConsecutiveSequence(arr2);
cout<<sum;
system("pause");
return 0;
}
分析:判断当前节点是属于一个序列的,只需判断前一个或者后一个节点也在序列中即可,即判断array[i]-1和array[i]+1也是位于这个序列中即可。如果这样判断的话,就需要保存过程中的部分值,为此,可以采用哈希表来保存计算的中间值。
参考解答:
#include "stdafx.h"
#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;
struct Bound
{
int low;
int high;
Bound(int h=0,int l=0)
{
high=h;
low=l;
}
};
int lengthOfLongestConsecutiveSequence(vector<int> &num)
{
unordered_map<int,Bound> table;
int local;
int maxLen=0;
for(int i=0;i<num.size();i++)
{
if(table.count(num[i])){
continue;
}
local=num[i];
int low=num[i],high=num[i];
if(table.count(local-1)){
low=table[local-1].low;
}
if(table.count(
9f02
local+1))
{
high=table[local+1].high;
}
table[low].high=table[local].high=high;
table[high].low=table[local].low=low;
if(high-low+1>maxLen)
{
maxLen=high-low+1;
}
}
return maxLen;
}
int _tmain(int argc, _TCHAR* argv[])
{
int arr[]={31,6,32,1,2,4};
vector<int> arr2(6);
for(int i=0;i<6;i++)
{
arr2.push_back(arr[i]);
}
int sum=lengthOfLongestConsecutiveSequence(arr2);
cout<<sum;
system("pause");
return 0;
}
相关文章推荐
- 获取一个数组中最长的连续的元素序列。例如,给定了[31,6,32,1,3,2],最长的连续的元素序列是[1,2,3],返回其长度3
- 一个无序整数数组中找到最长连续序列(Longest Consecutive Sequence)和两个元素使得相差最小
- 编程实现:从字符中获取连续数组序列,如字符串"a1dl2iad9j3la5kudp7u9pn4blj8ap5u3e6ml9a"中可以得到的连续数字序列为1234556.【注】:只考虑一位,因此最长的数
- 对于一个数组{1,2,3}它的子数组有{1,2},{1,3}{2,3},{1,2,3},元素之间可以不是连续的,对于数组{5,9,1,7,2,6,3,8,10,4},升序子序列有多少个?
- 给定一个长度为N的数组,找出一个最长的单调自增子序列(不一定连续,但是顺序不能乱).
- 对于一个数组{1,2,3}它的子数组有{1,2},{1,3}{2,3},{1,2,3},元素之间可以不是连续的,对于数组{5,9,1,7,2,6,3,8,10,4},升序子序列有多少个?
- 给定一个长度为N的数组,找出一个最长的单调自增子序列(不一定连续,但是顺序不能乱). 第二解
- 算法题:找出一个数组中相加值最大的连续序列元素
- 给一个数组,元素都是整数(有正数也有负数),寻找连续的元素相加之和为最大的序列。
- 求一个数组的最长递减子序列
- php数组函数序列之array_unshift() 在数组开头插入一个或多个元素
- 动态规划----求一个数组的最长增序列
- 利用Collections工具类获取字符串数组中最长的元素
- 【php数组函数序列】之prev() - 移动数组内部指针到上一个元素的位置,并返回该元素值
- 【php数组函数序列】之key() - 获取数组内部指针当前指向元素的键名
- 面试题:给定一个有序的数组,如果往该数组中存储一个元素,并保证这个数组还是有序的,那么元素的存储的角标位如何获取。
- 动态规划----求一个数组的最长递减序列
- 求一个数组的最长递减子序列 比如{9,4,3,2,5,4,3,2}的最长递减子序列为{9,5,4,3,2}
- 【php数组函数序列】之array_unshift() - 在数组开头插入一个或多个元素
- php数组函数序列之prev() - 移动数组内部指针到上一个元素的位置,并返回该元素值