http://ustraveldocs.com/这个网站那么烂啊# Reunion - 探亲与陪读t*n2014-04-13 07:041 楼find and print the first longest ascending or descending contiguoussubsequence for a vector of integers.
s*t2014-04-13 07:043 楼拿一个counter记住当前最长的ascending,再加一个maxlens,遍历一遍就好了吧。如果非要descending和ascending那就遍历的时候再加个变量记住吧。1 2 3 4 5 2 3 1 2 3 5 0对于只ascending,从左到右,比较。设置一个flag,标示现在是在递增。用counter记住到底增加了多少。如果递减了就counter清空。然后加一个max记住最长的。
s*t2014-04-13 07:045 楼我来写一下吧。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*o2014-04-13 07:047 楼这个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*n2014-04-13 07:049 楼谢谢!【在 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;}