t*********n 发帖数: 1292 | 1 find and print the first longest ascending or descending contiguous
subsequence for a vector of integers. |
s******t 发帖数: 2374 | 2 拿一个counter记住当前最长的ascending,再加一个maxlens,遍历一遍就好了吧。
如果非要descending和ascending那就遍历的时候再加个变量记住吧。
1 2 3 4 5 2 3 1 2 3 5 0
对于只ascending,
从左到右,比较。设置一个flag,标示现在是在递增。用counter记住到底增加了多少
。如果递减了就counter清空。
然后加一个max记住最长的。 |
s******t 发帖数: 2374 | 3 我来写一下吧。
int increaseCounter = 1;
int max = 1;
int maxIndex = 0;
for(int i = 1; i < a.length; i++){
if(a[i-1]< a[i]) increaseCounter ++;
else increaseCounter = 1;
if(increaseCounter > max){
max = increaseCounter ;
maxIndex = i-increaseCounter + 1;}
} |
r****o 发帖数: 1950 | 4 这个subsequence是一个一个挨着的,对吗?
【在 s******t 的大作中提到】 : 拿一个counter记住当前最长的ascending,再加一个maxlens,遍历一遍就好了吧。 : 如果非要descending和ascending那就遍历的时候再加个变量记住吧。 : 1 2 3 4 5 2 3 1 2 3 5 0 : 对于只ascending, : 从左到右,比较。设置一个flag,标示现在是在递增。用counter记住到底增加了多少 : 。如果递减了就counter清空。 : 然后加一个max记住最长的。
|
t*********n 发帖数: 1292 | 5 谢谢!
【在 s******t 的大作中提到】 : 我来写一下吧。 : int increaseCounter = 1; : int max = 1; : int maxIndex = 0; : for(int i = 1; i < a.length; i++){ : if(a[i-1]< a[i]) increaseCounter ++; : else increaseCounter = 1; : if(increaseCounter > max){ : max = increaseCounter ; : maxIndex = i-increaseCounter + 1;}
|