题目
给定长度为N的数组A[0...N-1],求递增且连续数字最长的子数组。
如数组:1,2,3,34,56,57,58,59,60,61,99,121的连续数字最长的一段位56,57,58,59,60,61.
代码
int MaxSequence(const int* a, int size, int& from, int& to)
{
int* p = new int[size];
int i;
for (i = 0; i < size; i++)
{
p[i] = 1;
}
int m = 1;
for (i = 1; i < size; i++)
{
if (a[i] - a[i - 1] == 1)
{
p[i] += p[i-1];
m = max(p[i], m);
to = i;
}
}
from = to - m + 1;
delete[] p;
return m;
}