问题描述:
样例输入:
代码如下:一次遍历搞定
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Fifth {
//最长连续递增子序列
public static List<Integer> increase(int[] res){
int len=1;//记录最长增持序列长度
int end=0;//记录结束的位置
int temp=1;//临时记录最长序列长度
boolean flag=true;//判断是否整体未递增序列
for (int i = 1; i <res.length ; i++) {
if (res[i]>res[i-1]){
temp++;
}else{
flag=false;
if (temp>len) {
len=temp;
end=i;
}
temp=1; //将序列清零
}
}
if (flag){//若全程都是递增的情况
len=res.length;
end=res.length;
}
if (temp>len){//防止最后的递增序列的bug
len=temp;
end=res.length;
}
System.out.println("--------"+end);
List<Integer> list=new ArrayList<>();
for (int i = end-len; i<end; i++) {
list.add(res[i]);
}
return list;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入数组的长度");
int len=scanner.nextInt();
int[] res=new int[len];
for (int i = 0; i <len ; i++) {
res[i]=scanner.nextInt();
}
List<Integer> list=increase(res);
System.out.println(list.toString());
}
}
运行结果如下: