数组A中存放很多数据,比如A={1,2,3,4,3,2,1,4,8,9,10};其中1,2,3,4/1,4,8,9,10都是递增子序列,1,4,8,9,10是最长的递增子序列。
寻找数组中的最长子序列,返回起始的索引值,如果没有递增子序列,那么返回-1.
实际就是连续判断A[i]是否比A[i-1]大,下面是我的代码:
寻找数组中的最长子序列,返回起始的索引值,如果没有递增子序列,那么返回-1.
实际就是连续判断A[i]是否比A[i-1]大,下面是我的代码:
int maxasds(int A[], int n)
{
int lastCount;
int count;
int max, idx;
int i;
max = 1;
idx = -1;
lastCount = 0;
count = 1;
for(i = 1; i < n; i++) {
if(A[i] > A[i - 1]) {
lastCount = 0;
count++;
} else {
lastCount = count;
count = 1;
}
if(max < lastCount) {
max = lastCount;
idx = i - lastCount;
}
}
return idx;
}
本文介绍了一种寻找数组中最长递增子序列的方法,并提供了一个实现该功能的C语言函数。通过遍历数组并比较相邻元素来确定递增序列,从而找出最长递增子序列及其起始位置。
&spm=1001.2101.3001.5002&articleId=83524983&d=1&t=3&u=df0b249d7a884db58ccc881438c1a7cc)

被折叠的 条评论
为什么被折叠?



